Skip to content

How to Fix TypeError in Python

DodaTech Updated 2026-06-15 1 min read

In this tutorial, you'll learn about How to Fix TypeError in Python. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Error message: TypeError: unsupported operand type(s) for +: 'int' and 'str'

Python raises TypeError when an operation or function is applied to an object of inappropriate type. It has several common variants.

Cause 1: Unsupported Operand Types

You're mixing incompatible types in an operation:

# Wrong
result = "Hello" + 5  # TypeError!

# Fix — convert to matching types
result = "Hello" + str(5)  # "Hello5"
result = len("Hello") + 5  # 10
# Another example
count = 10
print("Count: " + count)  # TypeError: can only concatenate str (not "int") to str
# Fix
print("Count: " + str(count))  # "Count: 10"

Cause 2: Object Is Not Callable

You're using parentheses () on something that isn't a function:

# Wrong — integer is not callable
x = 42
print(x())  # TypeError: 'int' object is not callable

# Fix — remove parentheses
print(x)  # 42
# Another case — variable shadows function
sum = 10  # Overwrites built-in sum()
print(sum([1, 2, 3]))  # TypeError: 'int' object is not callable
# Fix — don't name variables after built-in functions
total = 10
print(sum([1, 2, 3]))  # 6

Cause 3: Cannot Unpack Non-Iterable

You're trying to unpack a value that isn't iterable:

# Wrong
a, b = 42  # TypeError: cannot unpack non-iterable int object

# Fix — make it iterable
a, b = (42, 0)
# Wrong — function returns single value, expecting tuple
def get_point():
    return 3  # Should return (x, y)

x, y = get_point()  # TypeError: cannot unpack non-iterable int object

# Fix — return a tuple
def get_point():
    return (3, 4)
x, y = get_point()  # OK

Cause 4: Missing Arguments

def greet(name, greeting):
    return f"{greeting}, {name}"

greet("Alice")  # TypeError: missing 1 required positional argument: 'greeting'
# Fix
greet("Alice", "Hello")

Prevention

  • Use type hints and run mypy to catch type mismatches early
  • Before operations, check types with isinstance()
  • Don't shadow built-in names (sum, list, dict, str)
  • Convert types explicitly with str(), int(), float() when concatenating
  • Ensure functions return the expected number of values for unpacking

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro