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)Traceback (most recent call last): File "main.py", line 2, in <module> TypeError: can only concatenate str (not "int") to str
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))age: 12 age: 12 age: 12
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)5
Traceback (most recent call last): File "main.py", line 2, in <module> TypeError: can only concatenate str (not "int") to str
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().
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)Traceback (most recent call last): File "main.py", line 3, in <module> TypeError: can only concatenate str (not "int") to str
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)))123 1-2-3
What to check when you see this error
- The number after
linein 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. - If you are not sure, put
print(type(x))above that line.<class 'str'>is a string and<class 'int'>is an integer. - If you meant to join them, wrap the number in
str()or use an f-string. - If you meant to add them, convert the string with
int()orfloat(). Check whether the value came frominput(). - 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
- Python TypeError: unsupported operand type(s)
- Python ValueError: invalid literal for int()
- Syntax: Python f-string: Format Numbers and Text
- Syntax: Python print(): Output with sep and end
- Syntax: Python join(): Join a List into a String
- Syntax: Python input(): Read User Input
- Learn it step by step: Python Strings
- All Python error messages
- Online Python interpreter