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
+,-,*,/,//,%,**— every arithmetic operator refuses mixed number/text pairs. (*is the one exception in *spirit*:"ab" * 3repeats the string — but3 * "ab"also repeats it, and"a" * "b"still fails.)<,>,<=,>=— comparing a number with a string raises the comparison wording:'<' not supported between instances of 'int' and 'str'.&,|,^— bitwise operators need two integers.- The fix is always the same shape: convert one side so both operands speak the same type language.
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
- Read the two type names in the message — the left one is the left operand. Which one is the impostor?
- Print both operands before the line:
print(type(a), type(b)). - If you want text out, make both strings:
str(number) + text, or better, one f-string. - If you want maths out, make both numbers:
int(text)orfloat(text)— and be ready for a ValueError if the text is not a clean number.
Related messages worth knowing
- TypeError: the parent guide — all famous wordings of this exception decoded together.
- TypeError: can only concatenate str to str — the same bug when the string is on the left of the +.
- ValueError: invalid literal for int() — the error you meet next, converting dirty text to a number.
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 करो।