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.