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)}')
Output:
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)}')
Output:
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 using 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.
For further reading on TypeErrors involving ‘int’ objects, go to the articles:
- How to Solve Python TypeError: int() argument must be a string, a bytes-like object or a number, not ‘NoneType’
- Python TypeError: can only concatenate str (not “int”) to str Solution
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!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.