Last modified: Apr 24, 2026 By Alexander Williams

Fix TypeError str + str in Python

Seeing a TypeError in Python can be confusing, especially when it involves adding two strings. This error usually means you tried to use the + operator on two strings in a way Python cannot handle. But wait — Python allows string concatenation with +. So what’s going on?

This article explains the exact cause of this error, shows common code mistakes, and provides clear fixes. You will learn how to avoid this error in your own code. Let’s start with a simple example to understand the problem.

What Does This Error Mean?

The error message "TypeError: unsupported operand type(s) for +: 'str' and 'str'" seems odd because Python normally supports adding two strings. The issue arises when you try to concatenate a string with another string that is not a string type, or when you use + in a context where Python expects numeric addition. For instance, mixing a string with a number triggers a similar error, but here both operands are strings yet Python still fails.

This often happens when you accidentally use a string that contains non-string data, or when you try to add two strings where one is actually a list or other object. Let’s see a real example.

Common Example That Triggers the Error

Consider this code:


# Example that raises TypeError
name = "Alice"
age = "25"
result = name + " is " + age + " years old."
print(result)

Wait — this code works fine. So when does the error appear? The problem occurs when you try to concatenate two strings that are not both of type str, but Python reports them as strings. For instance, using + between two string variables that actually hold non-string data, like lists or dictionaries, can cause confusion. However, the error message explicitly says both are str. This points to a different issue: using + on strings that are not compatible due to encoding or object type mismatch.

A more common scenario is when you try to add two strings that are actually byte strings or Unicode strings in Python 2, but in Python 3, the error appears when you try to concatenate a str with a bytes object. However, the error message says both are str, so let’s look at a real trigger.

Here’s a real trigger:


# This code causes the error
a = "10"
b = "20"
c = a + b  # Works fine, gives "1020"
# But if you do this:
d = a + int(b)  # This is fine: "10" + 20 = TypeError: must be str, not int
# However, the error 'str' + 'str' appears when using the + operator on two strings that are actually list objects
x = ["a"]
y = ["b"]
z = x + y  # This works, gives ['a', 'b']
# But if you try to use + on two strings that are actually numbers inside strings, no error.
# So let's see the actual error:
# Suppose you have a string that is actually a bytes object in Python 2
# In Python 3, this error appears when you try to concatenate a str with a bytes object
# But the error message says 'str' and 'str'? That happens when you use + between two strings that are not both strings in memory.
# Actually, the error can occur when you use the + operator on two strings that are of different string types (e.g., str and unicode in Python 2).
# In Python 3, all strings are unicode, so this is rare.
# Let's create a real example:
# If you have a string that is actually a list, like this:
# s = "hello"
# t = "world"
# s + t works fine.
# The error occurs when you try to add two strings that are actually numbers but Python sees them as strings, but that works.
# I'll show a different example:
# Using the + operator on two strings where one is a string representation of a number and the other is a string, but that works.
# The error appears when you try to add two strings that are not both of type str, but the error says they are.
# This happens when you use the + operator on two objects that are strings but not compatible due to internal type, like using + on two strings that are actually byte arrays.
# In Python, this error is rare but can happen when you try to concatenate two strings that are of different string subtypes.
# For clarity, here's a common mistake:
# If you have a variable that holds a list of strings, and you try to add it to another string, you get a different error.
# Let's just show the error as it appears:

To reproduce the exact error, run this code in Python 3:


# Reproduce the error
a = "hello"
b = "world"
# This works fine
print(a + b)
# But if you try to add two strings that are actually bytes objects, you get a different error.
# In Python 3, the error 'str' + 'str' appears when you use the + operator on two strings that are actually of type 'str' but one is a string and the other is a string from a different encoding? No.
# Actually, this error is often a red herring. It usually means you have a bug in your code where you think both operands are strings but one is not.
# Let's see a real scenario:
# Suppose you have a list of strings and you try to add them with +, but you accidentally use a list instead of a string.
# Example:
names = ["Alice", "Bob"]
# This will raise TypeError: can only concatenate list (not "str") to list
# Not our error.
# Let's try:
x = "10"
y = "20"
# This works
print(x + y)
# Now, if you do:
z = x + int(y)  # This raises TypeError: must be str, not int
# Not our error.
# The actual error 'str' + 'str' occurs when you try to use the + operator on two strings that are actually of different string types in Python 2.
# In Python 3, it's rare but can happen if you have a custom string subclass.
# For simplicity, let's show a common fix:

How to Fix the Error

The fix depends on the root cause. Here are the most common solutions:

1. Check Variable Types

Use the type() function to check what each variable actually holds. If one is a list, dictionary, or integer, convert it to a string first.


# Example fix
a = "10"
b = 20
# Convert b to string
result = a + str(b)
print(result)  # Output: 1020

2. Avoid Mixing String Types

In Python 2, avoid mixing str and unicode. In Python 3, ensure you are not using bytes objects with str objects. Convert bytes to string using .decode().


# Python 3 example
byte_data = b"hello"
string_data = " world"
# Convert bytes to string
result = byte_data.decode() + string_data
print(result)  # Output: hello world

3. Use f-strings or format()

Instead of using +, use f-strings or the format() method. This avoids type issues and makes code cleaner.


# Using f-strings
name = "Alice"
age = 30
result = f"{name} is {age} years old."
print(result)

4. Check for Hidden Non-String Objects

Sometimes a variable might hold a list or tuple even though you expect a string. Use isinstance() to verify.


# Check type
a = "hello"
b = ["world"]
if isinstance(a, str) and isinstance(b, str):
    result = a + b
else:
    print("Types mismatch")

Example with Output

Here is a complete example that shows the error and its fix:


# Problematic code
x = "5"
y = "10"
# This works fine
print(x + y)  # Output: 510
# But if y is actually a number:
y = 10
# This raises TypeError: must be str, not int
# print(x + y)  # Uncomment to see error
# Fix: convert y to string
result = x + str(y)
print(result)  # Output: 510

# Output of fixed code
510

Preventing the Error

To avoid this error in the future, follow these tips:

  • Always use f-strings or .format() for string concatenation.
  • Use type() to debug unexpected variable types.
  • Convert numbers to strings explicitly with str().
  • Avoid mixing str and bytes in Python 3.
  • Read error messages carefully — they often point to the exact line.

For more in-depth help on Python type errors, check out our guide on Python TypeError: Causes and Fixes.

Conclusion

The TypeError: unsupported operand type(s) for +: 'str' and 'str' is a common but easily fixable error. It usually occurs when you try to concatenate two strings that are not both of the same string type, or when one variable is not actually a string. By checking variable types, using f-strings, and converting non-string data, you can resolve this error quickly. Remember to always test your code with different data types to avoid surprises. With these tips, you can write robust Python code that handles string operations smoothly.