Whether you are a newbie or an experienced programmer, you will occasionally encounter the “TypeError: unsupported operand type(s) for -: str and int” error when working with Python. If you are struggling with it, then continue reading the guide below to understand them.
Causes of the “TypeError: unsupported operand type(s) for -: str and int” error
This error occurs when you try to perform mathematical operations (addition, subtraction, multiplication, division) between a number and a string. Take a look at the code below to understand it better.
balance = 1000 withdrawal = input("How much are you withdrawing today? ") balance = balance - withdrawal print(f"Your balance: {balance}")
Output:
How much are you withdrawing today? 34.5
TypeError: unsupported operand type(s) for -: 'int' and 'str'
Two easy ways to fix this problem
Basically, you just don’t attempt to subtract between a string and an integer, and you can avoid this error.
Here are two straightforward ways to fix it:
Using int() function
Syntax:
int(value, base)
Parameters:
value
: A number that isn’t an int or a string.base
: The number format. Default: base = 10.
In the above example, the input()
function returns the entered data in string format. We will get an error when we take that input data and do a subtraction between string and an int.
The problem will be solved by converting the return value of the input()
to an int value using the int()
function. Like this:
balance = 1000 withdrawal = int(input("How much are you withdrawing today? ")) balance = balance - withdrawal print(f"Your balance: {balance}")
Output:
How much are you withdrawing today? 50
Your balance: 950
Using float() function
Syntax:
float(value)
Parameter:
value
: A number or a string that you want to convert to a floating point number.
We can solve the problem by replacing float()
for int()
in the example above. But the value returned between int()
and float()
is a different numeric type. So, depending on each case, choose to use float or int properly.
balance = 1000 withdrawal = float(input("How much are you withdrawing today? ")) balance = balance - withdrawal print(f"Your balance: {balance}")
Output:
How much are you withdrawing today? 50.0
Your balance: 950.0
Summary
The “TypeError: unsupported operand type(s) for -: str and int” error in Python is a problem that is very easy to fix. You just need to understand the data type of each variable in your entire program. Hope this article is helpful to you. Thanks for reading!
Maybe you are interested:
- UnicodeDecodeError: ‘ascii’ codec can’t decode byte
- TypeError: Object of type DataFrame is not JSON serializable in Python
- TypeError: ‘dict’ object is not callable in Python
- TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’
- TypeError: ‘str’ object cannot be interpreted as an integer

Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java