[Solved] typeerror: '<' not supported between instances of 'str' and 'int'

This article will solve the '<' not supported between instances of 'str' and 'int' error. This error occurs because string and integer can't be compared with comparison operators in Python.

Read Also: How to make an empty list in Python

Python is a statically typed language, therefore, when it comes to string and integer, they are two distinct data types. Hence, we cannot compare the two data types using comparison operators such as lesser than(<) directly without converting the two values into one data type.

To be more specific, to get rid of this error, you must manually transform the string to an integer. First, we will produce the error before moving on to the solution:

1. Producing the error

The below code will produce this error.

grade = input("Enter Students overall score: ")
if grade < 30: credentials = "Fail" elif grade < 60: credentials = "Pass" else: credentials = "First class" print(credentials)


Output:
Enter Students overall score: 80
Traceback (most recent call last):
File "<string>", line 3, in <module>
TypeError: '<' not supported between instances of 'str' and 'int'


Explanation:

If you look at the above code, we are trying to grade students based on the input of a user. If you check the output, the error is present since we are trying to compare grade which is expecting a data type of integer value and user input has a string value. When comparing these two values to determine a student's grade, the error occurs because the program cannot compare a string data type and integer data type. Now that we have TypeError: '<' not supported between instances of 'str' and 'int' in our code, let's look at the next section on how to solve this error.

1. Solution

To solve the type error, we will use the int() method. 

1. Using int() method


grade = int(input("Enter Students overall score: "))
if grade < 30: credentials = "Fail" elif grade < 60: credentials = "Pass" else: credentials = "First class" print(credentials)


Output:
Enter Students overall score: 80
First class


Explanation:

In the above code, we can see that we have used the int() method. This method converts user input which is a string data type into an integer data type, hence comparison can be made with comparison operators such as (<).

2. Producing the error

This error occurs when we try to compare two variables of different data types. Here, we are performing a comparison between integers and a String value.

print('The min is: ', min(1, 'hello', 7, 19))


Output:
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'int'


Explanation:

In the above code, we are finding the minimum number between the provided values.
We have provided 3 integer values and a string value. Then, we are using the min() function to find the minimum of these values. But when we execute(run) the program, we encounter this error.
The reason behind this error is that min() function is unable to compare the string value with integer values. As the "hello" is the only string among the integer values, the min() function is unable to compare it and throws the error.

2. Solution

To solve this error we will replace the string with integer as shown below:

print('The min is: ', min(1, 23, 7, 19))


Output:
The min is: 1

Explanation:

Here, min() function passed all integer arguments (1, 23, 7, 19) without any string values. As a result, the min() function is able to compare and find the smallest value among the integers. Hence, we got the output as 1.

That's all for today. Please mention in the comments in case you have any questions related to typeerror: '<' not supported between instances of 'str' and 'int'.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry