You can only compare items using mathematical operators if they have the same numerical data type. If you try to compare a string and an integer, you’ll encounter an error that says “not supported between instances of ‘str’ and ‘int’”.
In this guide, we discuss what this error means and why it is raised. We walk through an example scenario where this error is present so we can talk about how to fix it.
typeerror: ‘>’ not supported between instances of ‘str’ and ‘int’
Strings and integers cannot be compared using comparison operators. This is because strings and integers are different data types.
Python is a statically typed programming language. You have to manually change the type of a data if you need to compare it with a value of another type.
For instance, suppose you want to convert a string to an integer. You have to manually convert the string to an integer before the comparison will work.
The “typeerror: ‘>’ not supported between instances of ‘str’ and ‘int’” error occurs when you try to perform a “greater than” comparison on a string and an integer.
This error can happen with any comparison operation, such as a less than (<), less than or equal to (<=), or greater than or equal to (>=).
An Example Scenario
We build a program that calculates the letter grade a student has earned in a test.
To start, ask a user to insert a numerical grade which we will later convert to a letter grade using an input() statement:
numerical_grade = input("What grade did the student earn? ")
Then, use an “if” statement to calculate the student’s numerical grade:
if numerical_grade > 80: letter = "A" elif numerical_grade > 70: letter = "B" elif numerical_grade > 60: letter = "C" elif numerical_grade > 50: letter = "D" else: letter = "F" print(letter)
This code compares numerical_grade to a series of values. First, our code checks if numerical_grade is greater than 80. If it is, the value of “letter” becomes “A”. Otherwise, our code checks the next “elif” statement.
If the “if” and “elif” statements all evaluate to False, our code sets the value of “letter” to “F”.
At the end of our program, we print the letter grade a student has earned to the console.
Run our code and see what happens:
What grade did the student earn? 64 Traceback (most recent call last): File "main.py", line 3, in <module> if numerical_grade > 80: TypeError: '>' not supported between instances of 'str' and 'int'
Our code does not run successfully. Our code asks us to insert a student grade. When our code goes to make its first comparison, an error is returned.
The Solution
The input()
method returns a string. This means our code tries to compare a string (the value in “numerical_grade”) to an integer.
We solve this problem by converting “numerical_grade” to an integer before we perform any comparisons in our code:
numerical_grade = int(input("What grade did the student earn? "))
The int() method converts the value the user inserts into our program to an integer.
Try to run our code again:
What grade did the student earn? 64 C
Our code works successfully. This is because we now compare two integer values instead of an integer and a string.
Conclusion
The “typeerror: ‘>’ not supported between instances of ‘str’ and ‘int’” error is raised when you try to compare a string to an integer.
To solve this error, convert any string values to integers before you attempt to compare them to an integer. Now you’re ready to solve this common Python error in your code like a professional software developer.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.