0

So I tried putting this in VSCode:

character_name = "Mike"
character_age = 50
is_male = False
print("He really liked the name " + character_name + ", ")
print("but didn't like being " + character_age + ".")

However I would get this error message back:

TypeError: can only concatenate str (not "int") to str

Is there a workaround for this or should I just put the number in between quotation marks?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • In the print statement change `character_age` to `str(character_age)` – MDR Jul 13 '21 at 01:24
  • 1
    Does this answer your question? [How can I concatenate str and int objects?](https://stackoverflow.com/questions/25675943/how-can-i-concatenate-str-and-int-objects) – Ken Y-N Jul 13 '21 at 01:24
  • 1
    Instead of concatenating with `+` use a comma `,`. – John Gordon Jul 13 '21 at 01:26

3 Answers3

2

Alternatively, if you are using Python 3.6+, you could use f-strings, so your code could be written as:

character_name = "Mike"
character_age = 50
is_male = False
print(f"He really liked the name {character_name}, ")
print(f"but didn't like being {character_age}.")

Personally I prefer to do it this way as it is both a shorter syntax and it automatically applies str() to variables.

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19
1

The Wrong

Your mistake isnt the variable definition, its rather with line 5

print("but didn't like being " + character_age + ".")

Lets take a look at your error message

TypeError: can only concatenate str (not "int") to str

This is a TypeError, and it was raised because you cannot use "+" to add an int and a str, hence "TypeError"

The Why

An int (integer) is a number, whilst a str (string) is a word. Now, your error becomes obvious; It makes no sense to add a word and a number.

we can add two integers, or two strings together with the "+" operator, it has to be the same type.

Lets take a look here:

word = "this is a string"  # Making a string example
number = 5 # This is an example int

print(word + number)

And its output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

Here we get the same Error message, because we try to add an int and a str

Now, lets try a different example

print(5 + 5)  # Adding two integers
print("Hello " + "World!")  # Adding two strings
print(5 + "Hello!")  # Adding a string and an integer

And this example's output

>>> print(5 + 5)  # Adding two integers
10
>>> print("Hello " + "World!")  # Adding two strings
Hello World!
>>> print(5 + "Hello!")  # Adding a string and an integer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> 

Here, adding two numbers works, so do the strings. But, we get an error with the last one, adding a string and an int.

The Correct

We can just completely avoid using integers in this case, since we don't plan on adding that variable to another integer.

character_age = "50"  # Now its a string

And Heres the output

>>> character_name = "Mike"
>>> character_age = "50"
>>> is_male = False
>>> print("He really liked the name " + character_name + ", ")
He really liked the name Mike, 
>>> print("but didn't like being " + character_age + ".")
but didn't like being 50.

Or, if we needed to use integers, we can convert it to a string while we add them

print("but didn't like being " + str(character_age) + "."). # This will not affect the variable, but it will return a string version of character_age

And this has the same output

Another answer is to use an f-string in python 3.6+

>>> print("The restaurant received a {5} star rating")
The restaurant received a 5 star rating

You can read more about f-strings here

Conclusion

You need to convert the int value to a string for them to add, or just use a string in the first place. f-strings are an excellent way to do this without string concatenation and a simpler syntax.

0

Just convert the integer to string variable using str(character_age).

print("He really liked the name " + character_name + ", ")
print("but didn't like being " + str(character_age) + ".")

Or concatenate using commas

print("He really liked the name ", character_name, ", ", sep="")
print("but didn't like being ", character_age, ".", sep="")