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

Posted in /  

Python TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’ Solution
vinaykhatri

Vinay Khatri
Last updated on May 30, 2024

    Python supports arithmetic operators to perform arithmetic operations between two numerical values. But if we perform the subtraction - operation between a string and an integer value, we will encounter the TypeError: unsupported operand type(s) for -: 'str' and 'int' Error.

    In this Python Error guide, we will discuss this error and see how to solve it. We will also walk through a Python example that demonstrates this error, so you can get an idea about how to solve this error in Python programs.

    So let's get started with the Error statement.

    Python Error: TypeError: unsupported operand type(s) for -: 'str' and 'int'

    The Error Statement TypeError: unsupported operand type(s) for -: 'str' and 'int' is divided into two parts Exception Type and Error Message .

    1. Exception Type ( TypeError ): TypeError raises in Python when we try to perform an invalid operation on a Python data type object .
    2. Error Message( unsupported operand type(s) for -: 'str' and 'int' ): This error message is telling us  that we are performing the subtraction operation between an integer and string value using - Operator. And the subtraction operation is illegal between a string and an integer value.

    Python Error

    The main reason why we encounter TypeError: unsupported operand type(s) for -: 'str' and 'int' errors is when we try to subtract a string value and an integer value using the subtraction operator - .

    Python does not support the subtraction operation between a string value and an integer value, and if we try to perform it in our Program, we get the TypeError because Python is not capable of computing a string value with an integer value.

    Example

    # string number
    str_num = "34"
    
    # integer number
    int_num = 44
    
    # perfrom substraction (error)
    result = str_num -int_num
    
    print(result)

    Output

    Traceback (most recent call last):
    File "main.py", line 8, in <module>
    result = str_num -int_num
    TypeError: unsupported operand type(s) for -: 'str' and 'int'

    Common Scenario

    The most common scenario when many new Python learners encounter this error is when they input an integer value using the input function and forget to convert it into a int datatype. By default, the input() function returns the entered data into the string format. And when we use that input data with an integer value and perform the subtraction operation on them, we encounter this error.

    Example

    # integer value
    total_price = 40_000_000
    
    # string value
    downpayment = input("How much do you want to pay as a Down Payment: ")
    
    loan = total_price - downpayment
    
    print("Your Loan Amount is:", loan)

    Output

    How much do you want to pay as a Down Payment: 482739
    
    Traceback (most recent call last):
    File "main.py", line 7, in <module>
    loan = total_price - downpayment
    TypeError: unsupported operand type(s) for -: 'int' and 'str'

    Break the code

    In the above example, when we are asking the user to enter the downpayment value using the input() function, there we are acting the downpayment value as a string. And at line 7, we are subtracting the downpayment (string value) from the total_price (integer value) total_price - downpayment to compute the loan .

    As Python does not support subtraction operation between a string value and an integer value, that's why we receive the unsupported operand type(s) for -: 'int' and 'str' Error at line 7.

    Solution

    The solution for the above problem is very simple. All we need to do is convert the entered downpayment value to an integer value using the Python int() function. The Python int() function can convert a numeric string value to an integer value.

    Example Solution

    # integer value
    total_price = 40_000_000
    
    # integer value
    downpayment = int(input("How much do you want to pay as a Down Payment: "))
    
    loan = total_price - downpayment
    
    print("Your Loan Amount is:", loan)

    Output

    How much do you want to pay as a Down Payment: 20_300_200
    Your Loan Amount is: 19699800
    Note: In Python we can use underscore _ between numbers to write a integer value for better readibility, for example integer value 2_000 is equal to 2000, there is not difference.

    Wrapping Up!

    The Python Error unsupported operand type(s) for -: 'int' and 'str' is a TypeError that occurs when we perform the subtraction operation between an integer value and a string value.

    Python also does not support the addition operation between a string and an integer, so if you perform the addition operation between an integer and a string, you will get a similar error unsupported operand type(s) for +: 'int' and 'str' .

    If you are still getting this error in your Python program, please share your code in the comment section, and we will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments