/errors/python/typeerror-unsupported-operand-types-int-and-str
TypeError: unsupported operand type(s) for +: 'int' and 'str'
- tool
- Python
- exit code
- 1
- versions
- Python 3.x
- os
- any
TypeError: unsupported operand type(s) for +: 'int' and 'str' means Python tried to add a number to a string and neither type knows how to do it. Python never converts between text and numbers on its own, so you convert one side explicitly. The two type names in the message are the types of the left and right operands, in that order, which tells you which side is wrong.
Reproduce it
python3 -c "print(5 + '3')"Output · exit 1
Traceback (most recent call last): File "<string>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str'
The traceback ends with the message above. If the order is reversed ('3' + 5), the message becomes can only concatenate str (not "int") to str, which has its own page.
Where the text comes from
Strings that look like numbers usually arrive from outside the program:
input()always returnsstr, even when the user types digits.- CSV readers (
csv,pandas.read_csvwith mixed columns) and environment variables (os.environ) return text. - JSON values written as
"42"stay strings afterjson.loads. - Command-line arguments in
sys.argvare strings.
Check the type with type(value) or repr(value): '3' with quotes is text, 3 without quotes is a number.
Fix 1: convert the text to a number
python3 - <<'PY'
age = '3'
print(5 + int(age))
print(5 + float('2.5'))
PYOutput
8 7.5
int() only accepts whole numbers. For decimals use float() or decimal.Decimal, and handle ValueError when the text may be invalid (see the linked ValueError page).
Fix 2: convert the number to text
When the goal is a message, not arithmetic, use an f-string. It formats any value.
python3 - <<'PY'
count = 5
print(f"items: {count}")
print("items: " + str(count))
PYOutput
items: 5 items: 5
Other operand pairs
The same error appears with other type names, and the type names tell you what went wrong:
| Message | Meaning |
|---|---|
for +: 'int' and 'NoneType' | A function returned None instead of a number. |
for +: 'int' and 'list' | A list was used where one item was expected. |
for -: 'str' and 'str' | Strings do not support subtraction; convert both to int. |
for /: 'str' and 'int' | Text in a division; convert it with int() or float() first. |
Prevent it
Convert values once, where they are read, and keep numbers as numbers inside the program. Type hints plus a checker such as mypy or pyright report int + str before the code runs. In data frames, use astype(int) or pd.to_numeric(..., errors="raise") so bad rows fail early with a clear message.
FAQ
Why is the message different when the string comes first?
'abc' + 1 raises TypeError: can only concatenate str (not "int") to str. That message comes from str.__add__, while the int-first form comes from the generic operator lookup. Both have the same fix.
Does the message change for other operators?
Yes. The operator and the type names are filled in: for example unsupported operand type(s) for -: 'str' and 'int', or for *: 'NoneType' and 'int'.
Why does 'a' * 3 work but 'a' + 3 does not?
Repeating a string by an integer is defined for str, but adding a number to a string is not. Python does not convert types implicitly in arithmetic.