← Back

Home › Errors › TypeError

TypeError: unsupported operand type(s) for +: 'int' and 'str' — decoded

Exception: TypeErrorCategory: runtimeRaised at: when an operator sees mixed types

What this message means

Every operator asks its operands: *do you support this operation?* For +, numbers say yes to numbers, strings say yes to strings — and nobody says yes to the mixed pair. When the left operand is a number and the right one is a string, Python reports the exact pair it could not match, in order: 'int' and 'str'.

This is the same bug as can only concatenate str (not "int") to str — that wording appears when the *string* is on the left. The message you get simply depends on which side came first.

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    total = quantity + " kg"
             ~~~~~~~~~^~~~~~
TypeError: unsupported operand type(s) for +: 'int' and 'str'

A real example

quantity = 3
label = quantity + " kg"     # number + text — no rule matches
print(label)

Two honest fixes, depending on what you wanted:

label = str(quantity) + " kg"  # you wanted text: "3 kg"
label = f"{quantity} kg"        # same result, clearer

result = quantity + 3          # you wanted maths: 6 — keep both sides numeric

Which operators share this rule

The input() trap

The classic source of a hidden string where you think a number is: input(). It returns text even when the user pressed only digit keys, so the very first maths line after it explodes:

price = input("Price: ")   # user types 40 — price is "40"
count = 3
total = count + price       # TypeError: unsupported operand type(s) for +: 'int' and 'str'

total = count + int(price)  # convert once, at the boundary

The habit that kills this whole bug family: convert at the boundary — wrap input() in int() or float() the moment data enters, and keep it numeric everywhere inside.

The fix, step by step

  1. Read the two type names in the message — the left one is the left operand. Which one is the impostor?
  2. Print both operands before the line: print(type(a), type(b)).
  3. If you want text out, make both strings: str(number) + text, or better, one f-string.
  4. If you want maths out, make both numbers: int(text) or float(text) — and be ready for a ValueError if the text is not a clean number.

Related messages worth knowing

Fix it interactively

These free problems are all mixed-type arithmetic wearing different masks — fix them in the browser:

Practice more Python bugs →
🇮🇳 Hindi में समझें

हर operator अपने दोनों operands से पूछता है: "क्या तुम यह operation support करते हो?" Numbers अपने साथ numbers में काम करते हैं, strings अपने साथ strings में — mixed pair पर कोई rule नहीं, इसलिए TypeError। Fix हमेशा एक ही है: एक side को convert कर दो — text चाहिए तो str(), maths चाहिए तो int()/float()। सबसे common छुपा हुआ कारण input() है, जो digits type करने पर भी string return करता है — इसलिए input लेते ही int() में convert करो।

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