Typeerror unsupported operand type s for str and int

When working in Python, you might encounter an error message “Typeerror unsupported operand type s for str and int”.

This happens when you are trying to perform an operation between a string and an integer.

But don’t worry, because this article will help you to resolve that error.

In this article, we will discuss the “Typeerror unsupported operand type s for str and int”, provide the causes of this error, and give solutions to troubleshoot the error.

So first, let’s discuss what this “unsupported operand type s for str and int” TypeError means.

What is Typeerror unsupported operand type s for str and int?

The “Typeerror unsupported operand type s for str and int” is an error message indicating that you are trying to perform an operation between a string (type “str”) and an integer (type “int”).

This type of operation is not supported.

This is because a string and an integer are separate data types.

Operand Type(s)

“Operand types” is a phrase in computer programming that refers to the data types of the values being operated on by an operator.

For example, in the addition operation “1 + 2”, the operand types would be integer or numeric because the values being added are numbers.

In a string concatenation operation like “hello” + “world”, the operand types would be strings because the values being concatenated are text.

Why does “Typeerror unsupported operand type s for str and int” occurs?

The “Typeerror unsupported operand type s for str and int” error usually occurs when you try to perform an operation that is not supported between a string and an integer data type.

Here are the example scenarios:

Scenario 1:

We try to assign an integer and string value to the variables.

Then we try to add them using the + operator

x = 2
y = '6'

# Add two values
z = x + y
print('total: ',z)

In this example, we are assigning the integer value 2 to the variable x and the string value “6” to the variable y.

Then we are trying to add x and y together using the + operator and assign the result to the variable z.

However, since x is an integer and y is a string, this will result in an error:

Typeerror unsupported operand type s for str and int

Scenario 2:

Let’s create a spending application that tracks the remaining money of a user after making a purchase.

To start, we will prompt the user for their budget and stores the input as an integer.

budget = int(input("How much is your budget for this month? "))

The int() function is used to convert the user’s input (which is initially a string) into an integer.

Next, we will prompt the user for the name of the purchase they made and stores the input as a string.

purchase = input("What did you purchase? ")

After that, we will prompt the user for the price of the purchase they made and stores the input as a string.

price = input("How much was this purchase? ")

And then, we will subtract the price of the purchase from the budget to calculate how much money the user has left.

It stores the result as an integer

money_left = budget - price

And lastly, we will use string formatting to print the value of the money left.

print("You have ${} left in your budget.".format(money_left))

However, when we run this code we will encounter an error message stating:

Typeerror unsupported operand type s for str and int

This is because the variable price is stored as a string.

Then it is subtracted from the integer variable budget.

In Python, you cannot subtract a string from an integer, which is what causes the error.

So now let us know how to fix the “unsupported operand type s for str and int” error.

How to solve Typeerror unsupported operand type s for str and int?

Here are the alternative solutions that you can use to fix the “Typeerror unsupported operand type s for str and int” error:

Solution 1: Convert the string to an integer:

To perform arithmetic operations between a string and an integer, you need to convert the string to an integer first.

You can do this using the int() or float() function.

Here is the updated code for the scenario 2 example problem above:

budget = int(input("How much is your budget for this month? "))
purchase = input("What did you purchase? ")
price = int(input("How much was this purchase? "))
money_left = budget - price
print("You have ${} left in your budget.".format(money_left))

In this updated code, we converted the price variable to an integer using the int() function before subtracting it from the budget variable.

Solution 2: Convert the integer to a string:

If you need to concatenate a string and an integer, you can convert the integer to a string using the str() function.

Here is an example

age = 25
message = "I am " + str(age) + " years old."
print(message)

In this example, we converted the age variable to a string using the str() function before concatenating it with the string “I am “ and the string ” years old.”.

This allows us to create a single string containing both the string and the integer.

Output:

I am 25 years old.

Solution 3: Use the filter() function along with the str.isdigit() method:

If you need to extract an integer from a string in Python.

You can use the filter() function along with the str.isdigit() method to extract only the digits from the string.

Then convert the resulting string of digits to an integer using the int() function

For example:

string_with_digits = "I have 3 apples and 5 oranges"
digits_only = filter(str.isdigit, string_with_digits)
integer = int(''.join(digits_only))
print(integer)

In this example, we first define a string string_with_digits that contains a mixture of letters and digits.

Then we use the filter() function along with the str.isdigit() method to extract only the digits from the string.

The filter() function creates an iterator that contains only the digits from the string.

The join() method is used to concatenate the digits into a single string.

Finally, we convert the resulting string of digits to an integer using the int() function and print the integer value.

Output:

35

Solution 4: Check what type of variable stores:

In Python, you can use the built-in type() function to check the type of a variable.

The type() function returns the type of the variable as a string.

Here’s an example:

x = 5
y = "hello"
z = [1, 2, 3]

print(type(x))
print(type(y))
print(type(z))

The output of this code would be:

<class 'int'>
<class 'str'>
<class 'list'>

So those are the alternative solutions that you can use to fix the “Typeerror unsupported operand type s for str and int” error.

I hope one or more of them helps you to fix your problem.

Here are the other fixed Python errors that you can visit, you might encounter them in the future.

Conclusion

In conclusion, in this article, we discuss “Typeerror unsupported operand type s for str and int”, provide its causes and give solutions that resolve the error.

By following the given solution, surely you can fix the error quickly and proceed to your coding project again.

I hope this article helps you to solve your problem regarding a Typeerror stating “unsupported operand type s for str and int”.

We’re happy to help you.

Happy coding! Have a Good day and God bless.

Frequently Asked Questions

What is Python TypeError and what causes it?

TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.

How do I quickly debug a Python TypeError?

Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.

Should I catch TypeError or let it propagate?

For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.

How do I prevent TypeError in production?

Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.

Where can I find more TypeError fixes?

Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.

John Paul Blauro

Programmer & Technical Writer at PIES IT Solution

John Paul Blauro is a programmer and writer at PIES IT Solution, author of 55 Python error-fix tutorials at itsourcecode.com. Specializes in Python TypeError debugging (str/int type errors, unsupported operand types, iterable-related issues) and AttributeError debugging (NoneType, dict/list/series object attribute errors) for developers and BSIT students.

Expertise: Python · Python TypeError · Python AttributeError · Type Debugging · Error Handling  · View all posts by John Paul Blauro →