Solutions For 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 a standard error that appears with newbies. Fortunately, this error is relatively easy to fix with a few ways, like using int() or float() function. This article, the top, will help you understand the reason for the error and how to fix it with the most concise and easy-to-understand examples.

Why Is the error “TypeError: unsupported operand type(s) for /: str and str” in Python happening?

Your program has the error “TypeError: unsupported operand type(s) for /: str and str” in Python because when we calculate with operands with variables whose value is a string, the string is not supported to perform. Those calculations throw an error.

The error message occurs as follows:

TypeError: unsupported operand type(s) for /: 'str' and 'str'

Example

num1 = '3564'
num2 = '4567'

result = num1 - num2
print(result)

Output:

TypeError: unsupported operand type(s) for /: 'str' and 'str'

When we use ‘num1’ and ‘num2’ the variable has a value of string, we entered two numbers for the program to calculate these two variables. But after entering, it is not a number but a string, so the program throws the error.

How To Fix “TypeError: unsupported operand type(s) for /: str and str”?

Using the int() function

The int() function converts a string or some other value type to a number as long as the value of the string after the conversion does not generate an error or is a valid number.

Syntax

int(value, base)

Parameter

  • value: The value you want to convert to a number
  • base: Is the number format you want to convert to

we would use the int() function to get around the “TypeError: unsupported operand type(s) for /: str and str” error like this:

num1 = int('3564')
num2 =int( '4567')

result = num1 - num2
print(result)

Output

-1003

we used the int() function to convert the input values to a number and use it to calculate when our program no longer throws the error “TypeError: unsupported operand type(s) for /: str and str ” again.

Using the float() function

The float() and int() functions convert a variable to a number. Still, unlike the int() function, the float() function converts the value of that original variable to an actual number.

Syntax

float(value)

Parameter

  • value: The value you want to convert to a real number

we would use the int() function to get around the “TypeError: unsupported operand type(s) for /: str and str” error like this:

num1 = float('3564')
num2 = float('4567')

result = num1 - num2
print(result)

Output

-1003.0

After we use the float() function, we will get the input numbers converted from the string into numbers of the actual type, then get them to calculate and return the result that our program no longer has an error. “TypeError: unsupported operand type(s) for /: str and str”.

Summary

For the error “TypeError: unsupported operand type(s) for /: str and str” in Python wants it to no longer appear, we must ensure that when we use variables to use operands to calculate with variables, the variables are used must be variables with a value of a number and not variables with a value of the string. we hope this article helps you and your program good luck.

Leave a Reply

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