Skip to content

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 int and str in an arithmetic or concatenation operation

  • Called 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

✗ Broken code
age = 25
message = "I am " + age + " years old"
✓ Fixed code
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() or isinstance()

  • 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 mismatches

  • Validate function inputs with isinstance() before using them

Related Resources

Related Glossary

Related Lessons

Related Practice

← Back to language