TypeError > not supported between instances of float and str

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError > not supported between instances of float and str #

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

typeerror not supported between instances of float and str

Here is an example of how the error occurs.

main.py
my_float = 1.1 my_str = '2.2' # ⛔️ TypeError: '>' not supported between instances of 'float' and 'str' print(my_float > my_str)

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

To solve the error, make sure the values you are comparing are of compatible types.
main.py
my_float = 1.1 my_str = '2.2' # ✅ convert str to float print(my_float > float(my_str)) # 👉️ False

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

You can also use the str() class if you meant to compare 2 strings instead of 2 numbers.

If you use pandas, make sure to cast the items in the column to floats.

main.py
import pandas as pd d = {'col1': ['1.1', '2.2'], 'col2': ['3.3', '4.4']} df = pd.DataFrame(data=d) # ✅ cast items to float df['col1'] = df['col1'].astype(float) print(3.14 > df['col1'][0]) # 👉️ True

You can also use the str() class instead of float if you meant to compare 2 strings.

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
my_float = 1.1 from_input = input('Enter your fav number: ') print(from_input) # 👉️ 2.2 print(type(from_input)) # 👉️ <class 'str'> # ⛔️ TypeError: '<' not supported between instances of 'float' and 'str' print(my_float < from_input)

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 float() (or int()) class.

main.py
my_float = 1.1 from_input = input('Enter your fav number: ') print(from_input) # 👉️ 2.2 print(type(from_input)) # 👉️ <class 'str'> print(my_float < float(from_input)) # 👉️ True

We converted the string from the input function back to a float 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_float = 1.1 print(type(my_float)) # 👉️ <class 'float'> print(isinstance(my_float, float)) # 👉️ True my_str = '3.14' print(type(my_str)) # 👉️ <class 'str'> print(isinstance(my_str, str)) # 👉️ 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 'float' and 'str'" occurs when we use a comparison operator between values of type float and str. To solve the error, convert the string to a float before comparing, e.g. my_float > float(my_str).

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.