In Python, we can only compare objects using mathematical operators if they are the same numerical data type. Suppose you use a comparison operator like the greater than the operator or >, between a string and an integer. In that case, you will raise the TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’. This article will go through the error in detail, an example, and how to solve it.

TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’

Python is a statically typed programming language, which means you have to change the type of a value before comparing it to a value of a different type. In the case of a string and an integer, you have to convert the string to an integer before using mathematical operators. This particular TypeError is not limited to the “greater than” comparison and can happen with any comparison operator, for example, less than (<), less than or equal to (<=) or greater than or equal to (>=).

Example: Using the input() Function to Compare Numbers

You will typically encounter this error when using the input() function because it will return a string. Let’s look at an example of a program that takes an input and then tries to find the most significant number out of a collection of numbers, including the input.

# Input number

number = input("Enter a number to compare:  ")

# Print output

print(f'The maximum number is {max(2, 4, 5)}')

print(f'The maximum number is {max(2, number, 5)}')
Enter a number to compare:  20    

The maximum number is 5

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
print(f'The maximum number is {max(2, number, 5)}')

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

In the first part of the code, we pass three integers to the max function, which will find the maximum number, 5. However, in the second part of the code, we give a string to the max function with two other integers, we raise the TypeError:’>’ not supported between instances of ‘str’ and ‘int’. The error occurs when we compare two values whose data types are different, a string and an integer.

Generally, we raise a TypeError whenever we try to do an illegal operation for a particular object type. Another typical example is TypeError: ‘int’ object is not subscriptable, which occurs when accessing an integer like a list.

Solution

Instead of passing a string to the max function, we can wrap the input() function in the int() function to convert the value to an integer. The process is of converting a literal of one type is called type casting or explicit type conversion. We can use Python inbuilt functions like int(), float() and str() for typecasting.

# Input number 

number = int(input("Enter a number to compare:  "))

# Print output
 
print(f'The maximum number is {max(2, 4, 5)}')

print(f'The maximum number is {max(2, number, 5)}')
Enter a number to compare:  20

The maximum number is 5

The maximum number is 20

Our code now works successfully. The int() converts the string input to an integer to compare the two other integers.

Summary

Congratulations on making it to the end of this tutorial. To summarize, we raise the TypeError: ‘>’ not supported between instance of ‘str’ and ‘int’ when trying to use the greater than comparison operator between a string and an integer. This TypeError generalizes all comparison operators, not just the > operator.

To solve this error, convert any string values to integers using the int() function before comparing them with integers.

To learn more about Python, specific to data science and machine learning, go to the online courses page for Python.

Have fun and happy researching!