Last modified: Apr 23, 2026 By Alexander Williams

Python TypeError: Causes and Fixes

A TypeError in Python occurs when you perform an operation on an incompatible data type. It is one of the most common errors you will encounter as a beginner. Understanding it helps you write cleaner code and debug faster.

This error happens when Python cannot apply a function or operator to an object of a certain type. For example, adding a string to an integer raises a TypeError. Let's explore all things about this error in detail.

What is a TypeError?

A TypeError is an exception raised when an operation or function receives an argument of an inappropriate type. Python is dynamically typed, but it is still strongly typed. This means you cannot mix types without explicit conversion.

For instance, you cannot add a string and an integer directly. Python will raise a TypeError to tell you the types are incompatible.

Here is a simple example:

# Example of TypeError
result = "Hello" + 5  # This will raise TypeError

TypeError: can only concatenate str (not "int") to str

The error message clearly says you cannot concatenate a string and an integer. You must convert the integer to a string first.

Common Causes of TypeError

There are several common scenarios where TypeError appears. Knowing them helps you avoid the error in your code.

1. Mixing Types in Operations

This is the most frequent cause. You try to add, subtract, or compare values of different types.

# Mixing string and integer
num = 10
text = "20"
result = num + text  # TypeError

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

To fix this, convert the string to an integer using int() or the integer to a string using str().

2. Calling a Non-Callable Object

Another common cause is trying to call an object that is not a function. For example, using parentheses on an integer.

# Calling a non-callable
number = 42
number()  # TypeError

TypeError: 'int' object is not callable

This often happens when you accidentally name a variable the same as a function. Always check your variable names.

3. Using Wrong Arguments in Functions

Built-in functions expect specific types. If you pass the wrong type, you get a TypeError.

# Wrong argument type
print(len(42))  # TypeError

TypeError: object of type 'int' has no len()

The len() function expects a sequence like a string, list, or tuple. An integer is not a sequence.

4. Indexing with Wrong Type

When accessing items in a list or dictionary, you must use the correct key type. Lists require integers, while dictionaries can use strings or other hashable types.

# Indexing a list with a string
my_list = [1, 2, 3]
print(my_list["0"])  # TypeError

TypeError: list indices must be integers or slices, not str

For lists, always use integer indices. If you need to access by key, use a dictionary instead.

5. Iterating Over Non-Iterable Objects

Using a for loop on an integer or a non-iterable object raises a TypeError.

# Iterating over an integer
for i in 5:
    print(i)  # TypeError

TypeError: 'int' object is not iterable

To loop a specific number of times, use the range() function: for i in range(5).

How to Fix TypeError

Fixing a TypeError is usually straightforward. Follow these steps:

  • Read the error message carefully. It tells you what types are involved.
  • Check the types of your variables using the type() function.
  • Convert types explicitly using int(), str(), float(), or list().
  • Ensure you are using the correct function or method for the data type.

Here is a practical example:

# Fixing TypeError with type conversion
a = "100"
b = 200
# Convert a to int
result = int(a) + b
print(result)  # Output: 300

# Or convert b to str
result2 = a + str(b)
print(result2)  # Output: "100200"

300
100200

Always check the type of your data before performing operations. This simple habit saves a lot of debugging time.

TypeError with Lists

Lists are common sources of TypeError. Beginners often mix list types or use wrong indices. For more on list operations, read our Python List Operations Guide for Beginners.

One common mistake is trying to use a string as a list index. Another is adding a list to an integer.

# TypeError with lists
my_list = [10, 20, 30]
# Wrong index type
print(my_list["1"])  # TypeError

# Wrong operation
print(my_list + 5)  # TypeError

TypeError: list indices must be integers or slices, not str
TypeError: can only concatenate list (not "int") to list

To fix these, use integer indices and only concatenate lists with other lists. If you need to add an element, use the append() method.

Another common issue is when you try to access an index that does not exist. This is called an "IndexError", not a TypeError. Learn more in our guide Fix Python List Index Out of Range Error.

TypeError with Functions and Methods

When you call a function with the wrong number or type of arguments, Python raises a TypeError. For example, the sum() function expects an iterable of numbers.

# Wrong argument to sum()
print(sum("hello"))  # TypeError

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

The sum() function tries to add the characters, but strings cannot be added to integers. Use join() for strings instead.

Similarly, methods like .append() are only available for lists. Calling them on a string or integer gives a TypeError.

# Calling list method on a string
my_string = "hello"
my_string.append(" world")  # TypeError

AttributeError: 'str' object has no attribute 'append'

Note that this is an AttributeError, not TypeError. But it is related. Always use the correct method for the data type.

TypeError with Boolean Operations

Boolean values are a subtype of integers in Python. So True is 1 and False is 0. However, mixing booleans with strings or lists can still cause TypeError.

# Boolean with string
result = True + "hello"  # TypeError

TypeError: unsupported operand type(s) for +: 'bool' and 'str'

To fix this, convert the boolean to a string explicitly: str(True) + "hello". For more on booleans in lists, see Python List All True Booleans: Check & Create.

TypeError with Dictionaries

Dictionaries use keys, and keys must be hashable. Using a list as a dictionary key raises a TypeError because lists are mutable and unhashable.

# List as dictionary key
my_dict = {[1, 2]: "value"}  # TypeError

TypeError: unhashable type: 'list'

Use tuples instead of lists if you need a sequence as a key. Tuples are immutable and hashable.

How to Prevent TypeError

Prevention is better than fixing. Here are some tips:

  • Use type hints in your code to document expected types.
  • Check types with isinstance() before operations.
  • Use try-except blocks to handle TypeError gracefully.
  • Test your code with different data types.

Here is an example of safe coding:

# Safe addition with type checking
def safe_add(a, b):
    if isinstance(a, (int, float)) and isinstance(b, (int, float)):
        return a + b
    else:
        return str(a) + str(b)

print(safe_add(10, 20))    # Output: 30
print(safe_add("10", 20))  # Output: "1020"

30
1020

Using isinstance() helps you avoid unexpected TypeError in your programs.

Conclusion

The TypeError is a common but easy-to-fix error in Python. It happens when you mix incompatible data types or use wrong arguments. Always read the error message, check your variable types, and convert data when needed. With practice, you will handle these errors quickly and write robust code. Keep learning and happy coding!