← Back

Home › Errors › TypeError

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

  1. 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.
  2. Wrap the number in str(). str(age) when you must build the string with +.
  3. Pass them to print separately. print("Age:", age) — the comma inserts a space and prints any type.
  4. For lists of mixed items, " ".join(str(x) for x in items) converts every element first, because join refuses 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

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 को खत्म कर देती है।

पूरी Hindi explanation पढ़ें →