The Error
Your Python script crashes with:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Python saw you using + between an integer and a string and refused. Unlike JavaScript or PHP, Python never silently coerces types β you have to be explicit. This is actually a feature, not a bug. It prevents a whole class of subtle data corruption.
Common Scenarios
Scenario 1: Mixing user input with numbers
input() always returns a string β even when the user types 25:
age = input("Enter your age: ") # returns '25', not 25
years = 10
print("In 10 years you'll be " + age + years) # β TypeError
Scenario 2: Building strings with numeric variables
count = 42
message = "Total items: " + count # β TypeError
Scenario 3: Data from files, APIs, or databases
CSV files, JSON responses, and database drivers often return numeric-looking values as strings. This one trips up even experienced developers:
row = {"price": "99", "tax": "8"}
total = row["price"] + row["tax"] # returns "998", not 107 β string concat, not math
Quick Fix
Fix 1: Convert the integer to string for concatenation
Building a message? Wrap the number in str():
count = 42
message = "Total items: " + str(count) # β
"Total items: 42"
Fix 2: Convert the string to integer for arithmetic
Doing math? Convert with int() first:
age = input("Enter your age: ") # "25"
years = 10
future_age = int(age) + years # β
35
Fix 3: Use f-strings (cleanest approach)
F-strings embed values directly without any manual conversion. They're the modern Python way:
count = 42
message = f"Total items: {count}" # β
no str() needed
age = int(input("Enter your age: "))
years = 10
print(f"In 10 years you'll be {age + years}")
Permanent Fix: Handle Type Conversion Safely
Always convert input() immediately
The mistake most beginners make: reading input as a string, then forgetting to convert it twenty lines later. Convert at the source:
# Bad β easy to forget
raw = input("Enter price: ")
# ... 20 more lines of code ...
total = raw + 5 # β TypeError, far from where the bug started
# Good β convert on the spot
price = float(input("Enter price: ")) # β
price is a float from here on
total = price + 5 # β
10.0 if input was "5"
Validate and convert data from external sources
Wrap conversions in a helper when the data comes from outside your control:
def safe_add(a, b):
try:
return int(a) + int(b)
except (ValueError, TypeError) as e:
raise TypeError(f"Cannot add {type(a).__name__} and {type(b).__name__}: {e}")
row = {"price": "99", "tax": "8"}
total = safe_add(row["price"], row["tax"]) # β
107
Use f-strings instead of + for string building
String concatenation with + breaks the moment any value isn't already a string. F-strings never do:
# Fragile β every variable needs str() manually
result = "Score: " + str(score) + "/" + str(total) + " (" + str(pct) + "%)"
# Solid β types don't matter
result = f"Score: {score}/{total} ({pct}%)"
Debugging: Find Where the Type Mismatch Is
When the error happens deep inside your code, it's not always obvious which variable is the wrong type. Use type() to inspect:
a = get_value_from_somewhere()
b = get_another_value()
print(type(a), type(b)) # e.g.
result = a + b
Better yet, add assertions to catch mismatches before they cause confusing errors elsewhere:
assert isinstance(a, int), f"Expected int, got {type(a).__name__}: {a!r}"
assert isinstance(b, int), f"Expected int, got {type(b).__name__}: {b!r}"
Verify the Fix
Run this snippet to confirm everything works:
# Should catch the error, not crash
try:
result = 10 + "5"
except TypeError as e:
print(f"Error caught: {e}")
# Should print 15
result = 10 + int("5")
print(result) # 15
# Should print "Value: 10"
result = "Value: " + str(10)
print(result) # Value: 10
Both fixed snippets should print the expected output without raising any exception.
Summary
- Use
str(number)when building strings that include numbers. - Use
int(string)orfloat(string)when doing math with string values. - Prefer f-strings over
+concatenation β they handle types automatically. - Convert values from
input(), files, or APIs right when you read them, not pages of code later.

