When working with Python, you might encounter the error: TypeError: '<' not supported between instances of 'str' and 'int'. This typically occurs when you attempt to compare a string and an integer using comparison operators like <, >, or ==. This guide explains why this error occurs and how to resolve it with practical examples.
Typeerror: ‘>’ not supported between instances of ‘str’ and ‘int’
The comparison operator cannot be used to compare strings and numbers. This is due to the fact that string and integer are distinct data types.
Python is a computer language with a static type. If you need to compare it to a value of another kind, you must manually alter the data type. Assume you wish to convert a string to an integer. Before the comparison will work, you must manually convert the string to an integer.
When you try to do a “greater than” comparison on a string and an integer, the “typeerror: ‘>’ is not supported across versions of’str’ and ‘int'” occurs. This mistake can occur with any comparison operation, such as less than (), equal to (=), or more than (>=).
Error Example
If we have two integers, one an integer and the other a string, and we want to use the greater than operator to determine which is larger.
# 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 preceding example, x is a numeric string and y is an integer. At line 6, we can’t compare both values since x > y because Python doesn’t compare two different datatype values.
Solution
When comparing two data values, we must ensure that they are of the same data type. In the above example, we wish to compare two integers, x and y, thus we must first convert the text value to an int or float using the int() or float() functions.
# 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 aTypeerror: '<' not supported between instances of 'str' and 'int'
General example scenario
This isn’t the only time you’ll see this issue; there are other Python methods available, such as sort(), min(), max(), and so on, that employ the > greater than or less than operator to compare. Values should be compared. They also produce the same issue when we try to sort and find the greatest number in a list of integers that also contain a string value.
Example
If we have a list of prices comprising the prices of various items and we need to write a program that can determine the most costly product price from the list of prices.
# 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
The pricing list in the above example comprises 5 price values, the last of which is a string and the others are integers. Now when we use the max() function to identify the highest costly price in the list, we receive the error TypeError: ‘>’ not supported between instances of’str’ and ‘int’.
This is because, while comparing values, the method max() additionally employs the Greater than > operator to determine the biggest element. When it tried to compare the string value ‘999’ with another integer, it returned an error.
Solution
When using functions and methods that employ the back-to-back comparison operator, we must ensure that the values we pass are of the same data type. In our previous example, we can use the map() and int methods to convert all of the prices list entries to ints before using the max() function on them.
# 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
In this guide, we explored the reasons behind TypeError: '<' not supported between instances of 'str' and 'int' and provided practical solutions to resolve it.
If you encounter this or similar errors during development, feel free to reach out to us for assistance. With over a decade of experience in web development, mobile apps, and UX/UI design, ONEXT DIGITAL can provide the best solutions for your projects efficiently and effectively.




