TypeError: < not supported between instances of str and int

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: < not supported between instances of str and int #

The Python "TypeError: '<' not supported between instances of 'str' and 'int'" occurs when we use a comparison operator between values of type str and int. To solve the error, convert the string to an integer before comparing, e.g. int(my_str) < my_int.

typeerror not supported between instances of str and int

Here is an example of how the error occurs.

main.py
my_str = '10' my_int = 5 # ⛔️ TypeError: '<' not supported between instances of 'str' and 'int' print(my_str < my_int)

We used a comparison operator between values of incompatible types (str and int) which caused the error.

To solve the error, make sure the values you are comparing are of compatible types.
main.py
my_str = '10' my_int = 5 # ✅ convert str to int print(int(my_str) < my_int) # 👉️ False

We used the int() class to convert the string to an integer. You can also use the float() class if you need to convert the string to a floating-point number.

IMPORTANT: if you use the input() built-in function, all of the values the user enters get converted to strings (even numeric values).
main.py
from_input = input('Enter your fav number: ') print(from_input) # 👉️ 3 print(type(from_input)) # 👉️ <class 'str'> my_int = 5 # ⛔️ TypeError: '<=' not supported between instances of 'str' and 'int' print(from_input <= my_int)

The input function converts the data to a string and returns it.

Even if the user enters a number, it gets converted to a string before it is returned from input().

To solve the error, pass the string to the int() (or float()) class.

main.py
from_input = input('Enter your fav number: ') print(from_input) # 👉️ 3 print(type(from_input)) # 👉️ <class 'str'> my_int = 5 print(int(from_input) <= my_int) # 👉️ True

We converted the string from the input function back to an integer and compared the two numbers.

The values on the left-hand and right-hand sides of the comparison operator need to be of compatible types.

If you aren't sure what type of object a variable stores, use the built-in type() class.

main.py
my_str = '13' print(type(my_str)) # 👉️ <class 'str'> print(isinstance(my_str, str)) # 👉️ True my_int = 5 print(type(my_int)) # 👉️ <class 'int'> print(isinstance(my_int, int)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

Conclusion #

The Python "TypeError: '<' not supported between instances of 'str' and 'int'" occurs when we use a comparison operator between values of type str and int. To solve the error, convert the string to an integer before comparing, e.g. int(my_str) < my_int.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.