The TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’ error occurs when an integer is subtracted from a string in python. Auto casting is not possible in python. You can subtract a number from another number. If you attempt to subtract an integer value from a string containing a number, this error TypeError: unsupported operand type(s) for-: ‘str’ and ‘int’ will be thrown.

In python, an arithmetic operation can be used between the same data types. 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 number from a string that contains a number, the error TypeError: unsupported operand type(s) for-: ‘str’ and ‘int’ will be thrown.

Objects other than numbers can not be used in python substraction. The arithmetic subtract should only be used with numbers. If a number is stored as a string, it should be converted to an integer before subtracting from another number.



Exception

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

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 'int'
[Finished in 0.1s with exit code 1]


How to reproduce this error

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

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 'int'
[Finished in 0.1s 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 an integer is subtracted from a string, this error will be thrown. The string may contain a valid number. The string should be converted to integer before subtracting to an integer.



Solution 1

If you are attempting to subtract an integer 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 ‘int’ . You can not subtract an integer from a string.

x = 5
y = 2
print x - y

Output

3
[Finished in 0.1s]


Solution 2

If you try to subtract an integer from a 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 = 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 :
	print x - y
else :
	print "Not a number"	

Output

Not a number
[Finished in 0.0s]



Leave a Reply