TypeError: can only concatenate str (not "int") to str — decoded
Exception: TypeErrorCategory: runtimeRaised at: when + sees a str and a number
What this message means
The + operator does one thing per type pair. String + string concatenates. Number + number adds. String + number is not defined for any combination — Python refuses to guess whether you wanted "Age: 25" or 7, and raises this TypeError instead.
Read the message as a sentence about the *left* operand: the left side was a str, Python tried to concatenate onto it, and the right side (not "int") was the wrong type. When the left side is the number, the same bug produces a different wording — unsupported operand type(s) for +.
Traceback (most recent call last):
File "main.py", line 1, in <module>
print("Age: " + 25)
~~~~~~~~^^~~^
TypeError: can only concatenate str (not "int") to str
A real example
name = "Aarav"
age = 21
greeting = "Hi " + name + ", you are " + age + " today"
print(greeting)
Four + signs, one of them mixing a string with age. The fix that keeps this style is str(age) — but notice how noisy the corrected line already looks. That noise is the real argument for f-strings, below.
greeting = "Hi " + name + ", you are " + str(age) + " today" # works, but loud
The fixes, best first
- Use an f-string.
f"Hi {name}, you are {age} today"— any type interpolates, the code reads like the output, and there is no conversion to forget. - Wrap the number in
str().str(age)when you must build the string with+. - Pass them to
printseparately.print("Age:", age)— the comma inserts a space and prints any type. - For lists of mixed items,
" ".join(str(x) for x in items)converts every element first, becausejoinrefuses non-strings too.
age = 21
print(f"Age: {age}") # f-string — best
print("Age: " + str(age)) # str() — fine
print("Age:", age) # comma — also fine
The input() trap
input() always returns a string, even when the user types digits. So weight + 5 — where weight came from input() — is string + int and raises this exact error. The reverse mistake, int(input()) + " kg", produces the sibling message instead.
weight = input("Weight: ") # user types 70 — weight is "70"
total = weight + 5 # TypeError: can only concatenate str (not "int") to str
total = int(weight) + 5 # fix: convert once, at the boundary
The habit that prevents the whole family of bugs: convert at the boundary — the moment data enters your program — and keep numbers as numbers inside it.
Why Python refuses to auto-convert
Languages like JavaScript pick a rule and live with it: there "5" + 3 becomes "53". Python chose the opposite trade-off: explicit is better than implicit. A silent conversion would turn typos like zip_code + 1 into "3020" + "1" and hide the bug. The TypeError is annoying for one afternoon and protective for the rest of your career.
Related messages worth knowing
- TypeError: the parent guide — all five famous wordings decoded on one page.
- TypeError: unsupported operand types for + — the same bug when the number is on the left of the +.
- ValueError: invalid literal for int() — what happens when the str you convert is not a valid number.
Fix it interactively
These free problems are all this exact bug wearing different clothes — fix them in the browser and the reflex becomes automatic:
Practice more Python bugs →🇮🇳 Hindi में समझें
+ operator एक बार में एक ही type समझता है: string + string जुड़ती है, number + number जुड़ते हैं — पर string + number का मतलब Python खुद decide नहीं करता, इसलिए TypeError देता है। Fix: str(number) से convert करो, या बेहतर — f-string लिखो: f"Age: {age}"। एक और ज़रूरी बात: input() हमेशा string return करता है, इसलिए उसे उसी line पर int() में convert कर लो — यही एक आदत इस पूरे error-family को खत्म कर देती है।