Solutions To Solve The Error “TypeError: unsupported operand type(s) for -: str and str” In Python

The error “TypeError: unsupported operand type(s) for -: str and str” in Python is related to failed string processing. To understand it better, try the ways I will show in this tutorial.

Why does the error “TypeError: unsupported operand type(s) for -: str and str” in Python occur?

The reason Python is as popular as it is today is probably the biggest part because its syntax is so simple and easy to remember. However, because of that convenience, it causes certain troubles for users.

The error “TypeError: unsupported operand type(s) for -: str and str” in Python occurs when a user manipulates two strings together as a numeric data type operation. Example as follows.

classA = '40'
classB = '50'
students = classB - classA
print(f'Class A has {students} more students than Class B')

I have two classes, A and B. I want to calculate how many students in class B are more than the number in class A. However, I made a fundamental mistake in declaring the wrong data type. Instead of the number data type, I declared them as a string. So when I proceed to subtract 2 strings together, they give an unexpected error

Output

Traceback (most recent call last):
  File "D:\workspace\python\hello.py", line 3, in <module>
    students = classB - classA
TypeError: unsupported operand type(s) for -: 'str' and 'str'

How to fix it?

Re-initialize the variable

Implementing two string variables with number operators is risky. That’s why the fastest way to eliminate it is to re-initialize the variable with the data type number.

As the above example, I will initialize the variable as follows.

classA = 40
classB = 50
students = classB - classA
print(f'Class A has {students} more students than Class B')

Output

Class A has 10 more students than Class B

Simply like that, I calculated the amount of difference between the two classes.

Use int() method

Python supports many methods for converting data types. String-to-number conversion is possible and supported by Python. Below I will help you learn how to convert string to int in Python.

Syntax

int(param)
  • param: Is the parameter to be converted to the number int data type.

I will use the int() method for the above example to fix the error.

classA = '40'
classB = '50'

# Convertion data type
intClassA = int(classA)
intClassB = int(classB)

# Caculate and show the result
students = intClassB - intClassA
print(f'Class A has {students} more students than Class B')

Output

Class A has 10 more students than Class B

The conversion syntax is quite simple. Just passing the parameter and assigning it to another variable is successful.

Summary

In the above article, I helped you fix the “TypeError: unsupported operand type(s) for -: str and str” error in Python. Manipulation between data types is very important. If you need help understanding the principle of operation, it will be easy to cause errors, so please read the article carefully and follow each step to complete it. Thank you for reading

Leave a Reply

Your email address will not be published. Required fields are marked *