TypeError: unsupported operand type(s), what it means and how to fix it

Python refuses to guess how to combine unlike types. Adding a string to an integer, subtracting a list from a number, or multiplying text by a string raises this TypeError, and the message names the operator and both types involved, in order. The overwhelmingly common cause in beginner code is input(), which always returns a string, meeting an arithmetic operation that expected a number.

What it looks like

age = input("Age: ")      # input() always returns a string
next_year = age + 1

Traceback (most recent call last):
  File "main.py", line 2, in <module>
TypeError: unsupported operand type(s) for +: 'str' and 'int'

# fixed: convert the text to a number first
next_year = int(age) + 1

The fix, in order

  1. Read the two types in the message, left and right. The message is literal: the first named type is the left operand, the second is the right. One of them is not what you think it is; that is the whole diagnosis.
  2. Convert at the boundary, where data enters. Wrap input() immediately: age = int(input(...)) or float(...) for decimals. Data from files, JSON, and web forms is likewise text until converted.
  3. Print type(x) for the suspects when the source is unclear. Values that look numeric in output ("5" prints like 5) betray themselves only by type.
  4. Convert the other direction when the operation is really about text. Building a message from a number needs str(number) or an f-string: f"Total: {total}". Python will not concatenate a number into a string implicitly.
  5. For your own classes, the same error means an operator was used that the class does not define; implement __add__ and friends, or convert the object to a plain value first.

If it still happens

If the types in the message are types you never wrote (a library's classes), an API returned an unexpected shape, often None or a string where a number was promised. Inspect the actual returned object rather than the documentation's ideal version of it.

Common questions

Why does this happen so often with input()? Because input() returns a string by design, always, even when the user types digits. Every interactive beginner program meets this error exactly once per numeric field until converting at input becomes habit.

Other languages add a number to a string without complaint. Why not Python? Languages that coerce silently guess which way to convert, and guess wrong in ways that hide bugs (the classic “5” + 1 becoming “51”). Python's design refuses the guess and makes the programmer state the conversion, which is why the error exists.

Related errors

ValueError: invalid literal for int() · TypeError: 'int' object is not subscriptable · TypeError: 'str' object is not callable