{ }codelper

Error Messages › Python

Python TypeError: can only concatenate str

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

You get this error when you try to do string + number. + joins two strings and adds two numbers, but when the two are mixed Python cannot tell which one you want, so it stops. The not "int" in parentheses means the thing you tried to attach after the string was an integer. Look at what is on each side of the + on the line where the error happened.

Cause 1. You tried to print text and a number joined with +

Some other languages allow this, but Python does not turn a number into a string for you.

Wrong code

age = 12print("age: " + age)
When you run it
Traceback (most recent call last):
  File "main.py", line 2, in <module>
TypeError: can only concatenate str (not "int") to str

Edit and run it →

Fixed code

The easiest way is an f-string. You can also pass the parts to print separated by a comma (it puts one space between them), or turn the number into a string with str() and then join it.

age = 12print(f"age: {age}")print("age:", age)print("age: " + str(age))
Output
age: 12
age: 12
age: 12

Edit and run it →

Cause 2. You added a number to a value from input()

What input() returns is always a string. Even if you type 5, you get "5". Adding 1 to it is string + number.

Wrong code

n = input()print(n + 1)
Input
5
When you run it
Traceback (most recent call last):
  File "main.py", line 2, in <module>
TypeError: can only concatenate str (not "int") to str

Edit and run it →

Fixed code

Convert a value you will calculate with using int() as soon as you read it. For a number with a decimal point, use float().

n = int(input())print(n + 1)
Input
5
Output
6

Edit and run it →

Cause 3. You appended a number to a string in a loop

Using += with a number on a variable that started as the empty string "" gives the same error.

Wrong code

s = ""for i in range(1, 4):    s += iprint(s)
When you run it
Traceback (most recent call last):
  File "main.py", line 3, in <module>
TypeError: can only concatenate str (not "int") to str

Edit and run it →

Fixed code

Convert with str(i) each time you append. To join the pieces with a character in between, join is handy.

s = ""for i in range(1, 4):    s += str(i)print(s)print("-".join(str(i) for i in range(1, 4)))
Output
123
1-2-3

Edit and run it →

What to check when you see this error

  1. The number after line in the error message is the line where the error happened. See whether the left and the right of the + on that line are each a string or a number.
  2. If you are not sure, put print(type(x)) above that line. <class 'str'> is a string and <class 'int'> is an integer.
  3. If you meant to join them, wrap the number in str() or use an f-string.
  4. If you meant to add them, convert the string with int() or float(). Check whether the value came from input().
  5. If the order is the other way round (number + string), the error message changes to unsupported operand type(s) for +: 'int' and 'str'. The cause is the same.

Practice problems

See also