Integer values cannot be subtracted from string values and vice versa. This is because strings and integers are separate data types. If you try to subtract a string from an integer, you receive an error like “TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’”.
In this guide, we talk about the significance of this error and why it is raised. We walk through an example to help you figure out how to solve this error in your code.
TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’
Unlike other programming languages, Python syntax is strongly typed. One consequence of this is you have to change the types of objects, like strings and integers, if you want to treat them as a different type of data.
When you try to subtract a string for an integer or vice versa, Python does not know what to do. This is because you cannot subtract string values.
Similarly, you cannot add a string to an integer or divide a string by an integer. These operations all return an “unsupported operand type(s)” error.
An Example Scenario
We’re going to build a spending application that tracks how much money someone will have left on their budget after making a purchase. This application asks a user to insert the value of each purchase they make. This will be subtracted from the total amount a user has in their budget.
To start, ask a user to set a budget using the input() method:
budget = int(input("What is your budget for this month? "))
We have converted this value to an integer using the int()
method. Next, we ask a user to provide some details about their purchase. We ask about what they purchased and how much their purchase cost:
purchase = input("What did you purchase? ") price = input("How much was this purchase? ")
Next, we subtract the value of “price” from “budget”. This tells us how much a user has left in their budget.
We do this using the subtraction operator (-):
money_left = budget - price print("You have ${} left in your budget.".format(money_left))
Run our code to see if our program works:
What is your budget for this month? 400 What did you purchase? Monitor stand How much was this purchase? 35 Traceback (most recent call last): File "main.py", line 5, in <module> money_left = budget - price TypeError: unsupported operand type(s) for -: 'int' and 'str'
We’ve told our program our budget is $400 for the month. We have just purchased a monitor stand that cost $35. Our program fails to calculate our new budget. Let’s fix this error.
The Solution
To solve this error, we convert the value of “price” to a string.
By default, input()
returns a string. We changed the value of “budget” to be an integer earlier in our code. However, we did not change the value of “price”. This results in our code subtracting an integer from a string which is not possible.
Python cannot automatically convert a string to an integer because Python is statically typed.
We solve this error by replacing the “price” declaration with this code:
price = int(input("How much was this purchase? "))
We have surrounded the input()
statement with int()
. This makes the value stored in the “price” variable an integer. This converts the value a user inserts into our program to an integer. Run our code with this revised line of code:
What is your budget for this month? 400 What did you purchase? Monitor stand How much was this purchase? 35 You have $365 left in your budget.
Our code runs successfully. Our code subtracts 35 from 400. Our program then prints out how much money we have left in our budget to the console.
Similar Errors
There are a number of “unsupported operand type(s)” errors in Python.
These errors mean the same thing: you are trying to perform a mathematical operation on a string and a numerical value. Because strings do not support mathematical operations, you’ll encounter an error.
For instance, you see this error if you try to add a string and an integer:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Similarly, you see this error if you try to find the remainder of a string and an integer:
TypeError: unsupported operand type(s) for %: 'int' and 'str'
To solve this error in all cases, make sure you convert any string values to an integer before you use them in your code. You can do this using the int()
method.
Conclusion
The “TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’” error is raised when you try to subtract a string from an integer.
You solve this error by converting all strings to integers using the int()
method before performing a mathematical operation.
Now you’re ready to solve this common Python error like a professional developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot