Last modified: Dec 04, 2025 By Alexander Williams

Fix TypeError: 'int' and 'str' Python Error

This error is common in Python. It stops your code. It happens when you try to add a number and text. Python cannot do this directly.

You must convert one type to match the other. This guide explains why it happens. It also shows you how to fix it for good.

Understanding the TypeError

The plus operator + has two uses. It can add numbers. It can also join text strings. Python checks the data types first.

If you mix an integer and a string, Python gets confused. It does not know if you want math or text. So it raises a TypeError.


# This will cause the error
age = 25
message = "I am " + age + " years old."
print(message)
    

Traceback (most recent call last):
  File "script.py", line 3, in 
    message = "I am " + age + " years old."
TypeError: unsupported operand type(s) for +: 'int' and 'str'
    

The error message is clear. It says the types 'int' and 'str' cannot use the + operand together. You need to make them the same type.

Common Causes of the Error

This error often comes from user input. The input() function always returns a string. Even if the user types a number.


# User input is a string
user_age = input("Enter your age: ") # Returns a string like "30"
next_year_age = user_age + 1 # Error! Trying to add string and int
    

Another cause is reading data from files. Numbers in text files are strings. You must convert them before math operations.

Mixing data types in lists or variables is also a trigger. Always know your variable types. This prevents many errors like this one and others, such as Fix TypeError: slice indices must be integers.

Solution 1: Convert Integer to String with str()

Use the str() function. It converts an integer to a string. This is perfect for building text messages.


age = 25
# Convert the integer 'age' to a string
message = "I am " + str(age) + " years old."
print(message)
    

I am 25 years old.
    

Now the + operator concatenates three strings. The operation is valid. The output is correct.

Solution 2: Convert String to Integer with int()

Use the int() function for math. It converts a numeric string to an integer. Ensure the string can be converted.


user_input = "42" # This is a string
number = int(user_input) # Convert to integer
result = number + 10
print(result)
    

52
    

Warning: If the string is not a pure number, int() will fail. It will raise a ValueError. Always validate input first.

Solution 3: Use Formatted String Literals (f-strings)

f-strings are the modern way. They embed expressions inside string literals. You don't need manual conversion.


age = 25
# Use an f-string to format the message
message = f"I am {age} years old."
print(message)
    

I am 25 years old.
    

Python handles the type conversion automatically. It is clean and readable. This method is highly recommended.

Solution 4: Using the .format() Method

The str.format() method is another good option. It replaces placeholders {} with values.


score = 95
subject = "Math"
report = "Your {} score is {}.".format(subject, score)
print(report)
    

Your Math score is 95.
    

Like f-strings, .format() manages type conversion. It works in older Python versions too.

Handling User Input Safely

Always convert user input for calculations. Use int() or float(). Wrap it in a try-except block to catch errors.


try:
    user_age = input("Enter your age: ")
    age_int = int(user_age) # Attempt conversion
    next_year = age_int + 1
    print(f"Next year you will be {next_year}.")
except ValueError:
    print("Please enter a valid number.")
    

This prevents crashes from bad input. It's a key practice for robust programs. Similar care is needed for functions that may return None, as explained in Fix TypeError: 'NoneType' is Not Iterable.

Debugging Tips

Use the type() function to check variable types. This helps you understand what you are working with.


value = input("Enter a value: ")
print(type(value)) # Will always show 

Print variables and their types before the error line. This pinpoints the mismatch. Consistent type checking avoids many common Python TypeErrors, like those involving the len() function detailed in Fix TypeError: 'int' has no len() in Python.

Conclusion

The "unsupported operand" TypeError is a fundamental Python lesson. It teaches you about data types. The fix is always to ensure type compatibility.

Convert integers to strings with str() for text. Convert strings to integers with int() for math. Use f-strings for clean formatting.

Remember to validate user input. Use try-except blocks. Check types with type(). Mastering this error makes you a better Python programmer.