TypeError: unsupported operand type(s)
Error message
unsupported operand type(s) for +: 'int' and 'str'
Quick summary
You used a value with an operator or function that doesn't accept that type — like adding a string to an integer.
Why this happens
Mixed
intandstrin an arithmetic or concatenation operationCalled a function with an argument of the wrong type (e.g.,
len(5))Used an operator on types that don't support it
Minimal example
age = 25
message = "I am " + age + " years old"age = 25
message = "I am " + str(age) + " years old"Python can't concatenate str and int — convert age to a string with str(age) before concatenating.
How to diagnose
Read the error — it names the types and the operation involved
Check the types of the operands with
type()orisinstance()Trace where each value came from to find the type mismatch
How to fix
Convert the value to the correct type explicitly (e.g.,
str(age),int(s))Fix the variable that holds the wrong type at its source
Use
isinstance()checks before the operation
How to prevent
Use type hints (
def foo(x: int) -> str:) to catch mismatchesValidate function inputs with
isinstance()before using them
Related Resources
Related Lessons
- Data Types
Learn about Python data types
Related Practice
- Python Dynamic Typing
Understand Python's type system.