How to fix "TypeError: can only concatenate str (not "int") to str"
This error means you used + with a string on one side and a whole number on the other, and Python won't guess whether you wanted to join text or add numbers. If you're building a message, turn the number into text with str() or use an f-string, and if you meant to do arithmetic, turn the text into a number with int() or float().
Why it happens
+ does two different jobs. Between two numbers it adds, so 5 + 3 is 8. Between two strings it joins them, which is what "concatenate" means, so "5" + "3" is "53". When one side is a string and the other is an int, either answer could be what you wanted, so Python stops and tells you instead.
Here's the traceback from a two-line program that tries it.
laps = 12
print("Laps done: " + laps)
Traceback (most recent call last):
File "main.py", line 2, in <module>
print("Laps done: " + laps)
TypeError: can only concatenate str (not "int") to str
Read the last line first. It says a str can only be joined to another str, and you gave it an int. The line above shows the code that failed, and line 2 tells you where to find it. If you write it the other way round, as laps + " laps", the wording changes to unsupported operand type(s) for +: 'int' and 'str', but it's the same problem.
Run the example below to see the error for yourself. Then fix line 2 by wrapping laps in str(), and run it again. After that, try the f-string version, print(f"Laps done: {laps}"), which puts the number into the text for you. Both print Laps done: 12.
Run it
main.pylaps = 12
print("Laps done: " + laps)Output
Press Run and what your program prints shows up here.
Which fix is the right one
It depends on what the line is for. If you're printing a message, the number should become text, and an f-string is usually the tidiest way to do that.
If the string came from input(), you probably meant to do a sum. This gives exactly the same error, because input() always gives back text.
age = input("How old are you? ")
print(age + 1)
Here the fix goes the other way, with int(input("How old are you? ")), so that age holds a number. Picking the wrong fix swaps the crash for a wrong answer. age + str(1) runs without complaint, and if you type 14 it prints 141.
So when you see this error, look at the two things either side of the + and ask which type you wanted both of them to be.
A small exercise
This line is meant to print Total: 7, but it stops with the error from this page. Fix it.
print("Total: " + 3 + 4)
Show an answer
print("Total: " + str(3 + 4))
Or with an f-string, print(f"Total: {3 + 4}"). Python works through the + signs from left to right, so the first thing it tries is "Total: " + 3, and that's where it fails. If you wrap each number in str() separately, it runs but prints Total: 34, because the two digits get joined as text instead of added.
Reading the last line of a traceback first is the habit the Reading Tracebacks project builds, where you put Neil's race-day board back together from five scripts that each crash in a different way. The Handbook pages on TypeError and f-strings go into more detail.
The project that goes with it
Reading Tracebacks is a free project in Act 1, for Neil. It runs in your browser with nothing to install, and you don't need an account for it.