Python TypeError: '>' not supported between instances of 'str' and 'int' Solution

Posted in

Python TypeError: '>' not supported between instances of 'str' and 'int' Solution
vinaykhatri

Vinay Khatri
Last updated on April 25, 2024

    In Python, we have the > (greater than) operator, which is one of the six comparison operators. The greater than operator operates between two operands and checks if the operand on the left is greater than the operand on the right. It does so only if the two operands have a similar data type.

    If we use the greater than operator between a string value and an integer value, we will receive the error - " TypeError: '>' not supported between instances of 'str' and 'int' ".

    We will discuss this error in detail and learn how to fix it in this Python tutorial. We will also walk through some examples to help you know how to resolve this error.

    So let's get started!

    Python Problem - TypeError: '>' not supported between instances of 'str' and 'int'

    Python comparison operators can only compare data values of similar data types, and the Greater than operator is no exception.

    When comparing a string value with an integer value to find out which is greater, we encounter the error message: "TypeError: '>' not supported between instances of 'str' and 'int' Error".

    The above error Statement has two parts separated by a colon.

    1. TypeError
    2. '>' not supported between instances of 'str' and 'int'

    1. TypeError

    It is an exception type. It is raised in Python when we perform an unsupported operation or function on an inappropriate data type. Performing the greater than (>) comparison operation between a string and an integer raises the TypeError.

    2. '>' not supported between instances of 'str' and 'int'

    The " '>' not supported between instances of 'str' and 'int'" statement is the error message with the TypeError message. This error message tells us that Python does not support the > operation between a str and int instances. str and int are the default data type for string and integer data types, respectively.

    TypeError Example

    Let's say we have two numbers one is an integer, and the other is a string. We want to determine which one is greater using the greater than operator.

    # string number
    x = '20'
    # integer number
    y = 30
    
    if x > y:
        print("x is greater than y")
    else:
        print('y is greater than x')

    Output

    Traceback (most recent call last):
      File "main.py", line 6, in 
        if x > y:
    TypeError: '>' not supported between instances of 'str' and 'int'

    Break the code

    In the above example, x is a numeric string value and y is an integer numeric value. When we compare both values ( x > y) , Python throws the TypeError.

    Solution

    When comparing two data values, we need to ensure that both values have a similar data type. In the above example, we compare two numbers, x and y, which were int and str, respectively. So, before comparing them, we need to convert the string value to int or float using the int() or float() functions, resp.

    # string number
    a = '20'
    # integer number
    b = 30
    
    # convert string into int
    a= int(a)
    
    if a > b:
        print("a is greater than b")
    else:
        print('b is greater than a')

    Output

    b is greater than a

    Common Example Scenario

    This is not the only case when you encounter this error. There are some in-built Python methods, such as sort() , min() , max() , etc., that also use greater than (>) or less than (<) operators to compare values. And they also throw the same error when we want to sort or find the maximum number from a list of integers containing values of different data types.

    Example

    Let's say we have a list prices that contains the prices for different products. We will write a program to find the most expensive product.

    # list
    prices = [299, 449, 699, 899, '999']
    
    expensive = max(prices)
    
    print("Most expensive product price: ", expensive)

    Output

    Traceback (most recent call last):
      File "main.py", line 4, in 
        expensive = max(prices)
    TypeError: '>' not supported between instances of 'str' and 'int'

    Break the code

    In the above example, the prices list contains five price values, among which the last price value '999' is a string, and the rest of them are integers. When we apply the max() function on the prices list to find out the highest price, we receive the error  - TypeError: '>' not supported between instances of 'str' and 'int' .

    This is because, behind the scene, to compare the values, the max() function also use the greater than ( >) operator to find out the greatest element. And when it tries to compare the '999' string value with the other integer numbers, it results in an error.

    Solution

    Whenever we use such functions and methods that use a comparison operator in the background, we should ensure that the values we pass must have a similar data type. As far as our above example is concerned, we can convert all the prices list elements to the int using the map() and int function, then use the max() function.

    # list
    prices = [299, 449, 699, 899, '999']
    
    # convert all elements to integer
    prices = list(map(int, prices))
    
    expensive = max(prices)
    
    print("Most expensive product price:", expensive)

    Output

    Most expensive product price: 999

    Conclusion

    This was all about TypeError: '>' not supported between instances of 'str' and 'int'. It is a very common error, and it occurs when we try to compare a string value with an integer value. Additionally, many in-built Python functions and methods, such as sort() and max() , use comparison operators. They also return the same error when provided with values of different data types.

    Hence, if you receive this error, it is not necessary that you use the greater than (>) operator between a string and an int value. You may also receive it if you pass a list or tuple with a mix of integer and string values to the max() , sort() , and min() methods.

    Still, if you get this error in your Python program, you can share your code and query in the comment section. We will try to help you with debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments