/errors/python/typeerror-can-only-concatenate-str-not-int
Python TypeError: can only concatenate str (not "int") to str
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · Python 3.13.15updated
- tool
- Python
- exit code
- 1
- versions
- Python 3.x
- os
- any
TypeError: can only concatenate str (not "int") to str means the left side of + is a string and the right side is an int. Python does not convert types implicitly: "Age: " + 42 is an error, not "Age: 42". Convert the number with str(), or build the text with an f-string.
Reproduce it
cat > app.py <<'EOF'
age = 42
print("Age: " + age)
EOF
python app.pyOutput · exit 1
Traceback (most recent call last):
File "/tmp/work/app.py", line 2, in <module>
print("Age: " + age)
~~~~~~~~^~~~~
TypeError: can only concatenate str (not "int") to strThe text in parentheses names the type of the right operand, so the same error appears as (not "float"), (not "list") or (not "NoneType").
Fixes
python - <<'EOF'
age = 42
print("Age: " + str(age)) # explicit conversion
print(f"Age: {age}") # f-string, works for any type
print("Age:", age) # print converts and adds a space
print(int("40") + 2) # arithmetic: parse the string instead
try:
42 + "1"
except TypeError as e:
print("TypeError:", e)
EOFOutput
Age: 42 Age: 42 Age: 42 42 TypeError: unsupported operand type(s) for +: 'int' and 'str'
When the number is on the left (42 + "1"), the message is different (unsupported operand type(s) for +), but the cause and fix are the same: both operands of + must have compatible types.
Sources
Related pages
errors · pythonTypeError: 'NoneType' object is not subscriptablePython raises this TypeError when you index a value that is None, often the result of list.sort(), dict.get() or re.match(). Find where None comes from.errors · pythonUnboundLocalError: cannot access local variable 'x' where it is not associated with a valuePython raises UnboundLocalError when a function assigns to a name that is also global: the name becomes local. Fix it with global, nonlocal, or pass it in.compare · pythonPython is vs ==: identity vs equalityIn Python == compares values and is compares identity (the same object). Use is only for None and sentinel objects. Why 256 is 256 but 257 may not be.