[Fixed] TypeError: ‘<' not supported between instances of 'str' and 'int'

Introduction

Problem: How to fix – TypeError: '<' not supported between instances of 'str' and 'int' in Python?

Example:

Output:

Enter a number: 20
The minimum number is: 1
Traceback (most recent call last):
File “D:/PycharmProjects/PythonErrors/rough.py”, line 3, in
print(“The minimum number: “, min(5, num, 25))
TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’

Explanation:

  • In the above example, you can see that when three integers were passed to the min function, then the program gave us the output by fetching the minimum number, i.e., 1.
  • But, when we passed a string value to the min function along with two other integers, then it resulted in a Type Error: '<' not supported between instances of 'str' and 'int'.

Hence the TypeError occurred when we compared two variables whose datatypes are completely different (a string and integer in this case.)

Before proceeding further, let us have a quick look at some of the key concepts that will help you to identify and avoid TypeErrors in the future.

💡 Python TypeError

  • TypeError is one of the most common standard Python exceptions that occur in Python. Python throws a TypeError when an operation is carried out on an incorrect/unsupported object type. 
  • For example, using the < (less than) operator on an integer and a string value will throw a TypeError.
    • This is because integers and strings are two different types of data types and cannot be compared with the help of operators used for comparisons such as <, >, >=, <=, ===, !==.

📖 Read more here: What is TypeError in Python?

📍 The following example will be used to demonstrate the solutions to our mission- critical question:- How to fix TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’?

Example

The following table lists the tenure and the corresponding Fixed Deposit rates of a certain bank.

TenureFD Interest Rates
6 months to 364 days5.25%
18 months 1 day to 1 year 364 days6.00%
1 year 1 day to 18 months5.75%
2 years to 2 years 364 days6.00%
3 years to 4 years 364 days5.65%
5 years to 10 years5.50%

In the following code, when the user selects a certain tenure the corresponding FD rates shall be displayed. Also, if tenure is greater than 1 year 1 day to 18 months, then customer is a “Prime Customer” else “Normal Customer”.

Output:

The above error occurred because if option < 4 is an unsupported operation. Let’s solve this issue using the following methods.

📌 Method 1: Using int()

Instead of accepting the user input as a string, you can ensure that the user input is an integer with the help of int() method.

Solution:

Output:

Explanation: int(value) function converts the user-input to an integer value. Thus now you can compare two values of the same data type using the < operator.

📌 Method 2: Using a Custom Function

Instead of manually typecasting the string value to an integer you can also define a function to convert it to a string.

Example:

Output:

1. 6 months to 364 days
2. 18 months 1 day to 1 year 364 days
3. 1 year 1 day to 18 months
4. 2 years to 2 years 364 days
5. 3 years to 4 years 364 days
6. 5 years to 10 years

Select the Tenure (1 – 6): 5
Prime Customer
Rate = 5.65%

🐰 Bonus Read: Python Typecasting

Type Casting is the process of converting a literal of one type to another type. Typecasting is also known as explicit type conversion. Python inbuilt functions like int(), float() and str() are used for typecasting.

For example:

  • int() function can accept a float or string literal as an input and return a value of int type.

Output:

Conclusion

Hence, in this article, you learned what is Type Error: '<' not supported between instances of 'str' and 'int' and the possible causes and solutions of the error.

In general, if you are getting a TypeError in your code, take a moment to verify that the data you are operating on is actually of the type you would expect it to be.

You can rectify this error by manually converting the datatype so that the comparison is performed between values of the same datatypes.

Recommended Tutorials:

Please subscribe and stay tuned for more tutorials. Happy learning! 📚

Related Posts

  • Detect keypress in Python
    12 June

    Detect keypress in Python

    Table of ContentsUsing the keyboard module to detect keypress in PythonUsing the pynput module to detect keypress in PythonUsing the msvcrt module to detect keypress in Python Python allows us to work with user input in its programs. We can also work with hardware devices in Python. In this article, we will discuss how to […]

  • Copy a dictionary in Python
    12 June

    7 ways to copy a dictionary in python

    Table of ContentsUsing the copy() function to copy a dictionary in PythonUsing the deepcopy() function to copy a dictionary in PythonUsing the dict() function to copy a dictionary in PythonUsing the unpack operator ** to copy a dictionary in PythonUsing the update() function to copy a dictionary in PythonUsing the dictionary comprehension method to copy […]

  • Escape quotes in Python
    11 June

    Escape quotes in Python

    Table of ContentsUsing \ to escape quotes in PythonUsing \ with replace() to escape quotes in PythonUsing json.dumps to escape quotes in PythonUsing triple single or double quotes to escape quotes in pythonUse r to include \ and quotes in String 💡 Outline You can use \ to escape quotes in Python. If you want […]

  • Sleep for milliseconds in python
    31 May

    How to sleep for milliseconds(ms) in Python

    Table of ContentsUse time.sleep() function to make the program sleep for millisecondsUse threading.Timer() function to make the program sleep for milliseconds 💡 Outline You can use time.sleep() method to sleep for milliseconds in Python. If you want to delay python program by 500 ms, then you need to pass 0.5 as parameter to sleep method. […]

  • Convert unix timestamp to datetime in Python
    21 May

    Convert Unix timestamp to DateTime in python

    Table of ContentsUsing fromtimestamp() FunctionUsing strftime() FunctionUsing to_datetime() Function A UNIX timestamp is defined as the several numbers of seconds between a particular date and the Epoch i.e January 1, 1970 at UTC. In this tutorial, we will see some methods by which we can convert a UNIX timestamp to DateTime in Python. Using fromtimestamp() […]

  • Python round to 2 decimal places
    21 May

    How to round to 2 decimals in Python

    Table of ContentsUsing the round() functionUsing the format() functionUsing the % formattingUsing the f-stringsUsing the math.ceil() and math.floor() functions In Python, we deal with integers, floating values, and even imaginary numbers. Float numbers are used to represent the real numbers with integers and decimal parts. Python represents such numbers with 64-bit precision giving it a […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.