Python crashed with the error TypeError: can only concatenate str (not "int") to str
. Essentially Python's saying you've used +
between a string and a number and it's unhappy about it. Let's talk about how to fix this issue and how to avoid it more generally.
Table of contents
What does that error message mean?
The last line is in a Python traceback message is the most important one.
TypeError: can only concatenate str (not "int") to str
TypeError: can only concatenate str (not "int") to str
That last line says the exception type (TypeError
) and the error message (can only concatenate str (not "int") to str
).
This error message says something similar:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Both of these error messages are trying to tell us that we're trying to use the +
operator (used for string concatenation in Python) between a string and a number.
You can use +
between a string and a string (to concatenate them) or a number and a number (to add them).
But in Python, you cannot use +
between a string and a number because Python doesn't do automatic type coercion.
Type conversions are usually done manually in Python.
How do we fix this?
How we fix this will …