Skip to content

/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

bash
cat > app.py <<'EOF'
age = 42
print("Age: " + age)
EOF
python app.py

Output · 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 str

The text in parentheses names the type of the right operand, so the same error appears as (not "float"), (not "list") or (not "NoneType").

Fixes

bash
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)
EOF

Output

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