TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’

The python error TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’ occurs when two strings contain numbers that are subtracted from each other. Auto casting is not supported by python. You can subtract a number from a different number. If you try to subtract a string from another string that may contain a number, the error TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’ will be thrown.

In python, an arithmetic operation can be used between valid numbers. For example, you can subtract a number from a different number. The integer can be subtracted from a float number. If you try to subtract a string from a string that contains a number, the error TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’ will be thrown.

Objects other than numbers can not be used in python substraction. The arithmetic subtract can be used only for numbers. If a number is stored as a string, it should be converted to an integer before subtracting it from each string. If you try to subtract a string to a string containing a number, the error TypeError: unsupported operand type(s) for +: ‘str’ and ‘str’ will be shown.



Exception

The error TypeError: unsupported operand type(s) for-: ‘str’ and ‘str’  will be shown as below the stack trace. The stack trace shows the line where a string is subtracted from a string that may contain valid numbers.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x - y
TypeError: unsupported operand type(s) for -: 'str' and 'str'
[Finished in 0.0s with exit code 1]


How to reproduce this error

If you try to subtract a string from another string containing a number, the error TypeError: unsupported operand type(s) for-: ‘str’ and ‘str’ will be reproduced. Create two python variables. Assign variables with a string that contains a valid number. Subtract from the string to another string.

x = '5'
y = '2'
print x - y

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x - y
TypeError: unsupported operand type(s) for -: 'str' and 'str'
[Finished in 0.0s with exit code 1]


Root Cause

Python does not support auto casting. The arithmetic operation subtraction can be used to subtract between two valid numbers. If a string is subtracted from another string, this error will be thrown. The string may contain a valid number. The string should be converted to integer before subtracting.



Solution 1

If you try to subtract a string from another string contains a valid number, convert the string to an integer using the built in function int(). This will resolve the error. The built in function int() converts a string contains a valid number to an integer number.

x = int('5')
y = int('2')
print x - y

Output

3
[Finished in 0.1s]


Solution 2

If you are trying to subtract a string from a string containing a valid number, change the string to an integer. This is going to address the error TypeError: unsupported operand type(s) for-: ‘str’ and ‘str’ . You can not subtract a string from another string.

x = 5
y = 2
print x - y

Output

3
[Finished in 0.1s]


Solution 3

If the variable type is unknown, the variable type should be checked before subtracting the number to another number. The example below shows the method of verification before subtracting the numbers

x = '5'
y = '2'
if (x is int) and (y is int) :
	print x - y
else :
	print "Not a number"

Output

Not a number
[Finished in 0.0s]



Related Articles

Leave a Reply

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