Typeerror not supported between instances of str and int

One of the common errors in Python you might encounter is TypeError not supported between instances of str and int.

This error often appears when we perform an operation wherein it is not supported between two different data types, namely strings, and integers.

So, in this guide, we will discover what is this error and provide solutions to fix it.

What is Typeerror not supported between instances of str and int?

The Python “TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’” occurs when we compare the value type of a string and int using a greater than operator.

In order to fix the error, we need to convert the string to an integer before the comparison of values.

Here is an example of how this error occurs:

a = "10"
b = 5
if a < b:
    print("a is less than b")

This error occurs because we’re trying to compare a string (“10”) and an integer (5) using the less than (<) operator. Python doesn’t know how to compare these two different data types, so it raises a TypeError.

How to fix Typeerror not supported between instances of str and int

There are several ways to fix the TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’ error.

Convert the string to an integer to solve the error

One way to fix this error is to convert the string to an integer before comparing. You can do this by using the int() function.

a = "10"
b = 5
if int(a) < b:
    print("a is less than b")
else:
    print("a is greater than b")

Output:

a is greater than b

Convert the integer value to a string using the str() function.

One way to fix this error is to convert the integer to a string before comparing. You can do this by using the str() function.

a = "10"
b = 5
if a < str(b):
    print("a is less than b")

In this solution, we convert the integer 5 to a string using the str() function, so we can compare it with the string “10“.

Output:

a is less than b

Use a try-except block to catch the error and handle it appropriately

In this approach, we wrap the comparison in a try-except block to catch the TypeError that occurs when we try to compare a string and an integer.

a = "10"
b = 5
try:
    if a < b:
        print("a is less than b")
except TypeError:
    print("Cannot compare string and integer")

In this solution, we use a try-except block to catch the TypeError that occurs when we try to compare a string and an integer. We then print a custom error message to inform the user that the comparison is not possible.

Output:

Cannot compare string and integer

Ensure both operands are of the same type before comparing

It’s important to understand the data types you’re working with and how they can be manipulated before attempting to perform any comparisons.

a = "10"
b = "5"
if a < b:
    print("a is less than b")

In this solution, we make sure that both operands are of the same type (string), so we can compare them using the less than operator (<).

Output:

a is less than b

Call the min() and max() functions with an int and a string

The TypeError: not supported between instances of ‘str’ and ‘int’ also occurs when we call the min() and max() functions with an integer and a string.

Here is an example of how an error occurs:

a = 15
b = '20'

# TypeError: '<' not supported between instances of 'str' and 'int'
print(min([a, b]))

# TypeError: '>' not supported between instances of 'int' and 'str'
print(max([b, a]))

We can fix this error by converting them to an integer.

a = 15
b = '20'

print(min([a, int(b)]))

print(max([int(b), a]))

Meanwhile, we can convert all in the list into integers using list comprehension. Where List comprehension is used to perform some operation for every element or select a subset of elements that meet a condition.

Frequently Asked Question (FAQs)

How do I fix error not supported between instances of str and int?

To fix the TypeError: not supported between instances of ‘str’ and ‘int’ error, we need to convert the string to an integer before the comparison of values

What is not supported between instances of str and int error in Python?

This not supported between instances of str and int error appears when we perform an operation wherein it is not supported between two different data types, namely strings, and integers.

Conclusion

In conclusion, this TypeError: not supported between instances of ‘str’ and ‘int’ be fixed by converting the string to an integer before the comparison of values.

We hope that this guide has helped you resolve this error and get back to coding.

If you are finding solutions to some errors you might encounter we also have Typeerror: nonetype object is not callable.

Thank you for reading!

Frequently Asked Questions

What is Python TypeError and what causes it?

TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.

How do I quickly debug a Python TypeError?

Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.

Should I catch TypeError or let it propagate?

For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.

How do I prevent TypeError in production?

Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.

Where can I find more TypeError fixes?

Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.

Glay Eliver

Programmer & Technical Writer at PIES IT Solution

Glay Eliver is a programmer and writer at PIES IT Solution, author of over 600 tutorials at itsourcecode.com. Specializes in JavaScript tutorials, Microsoft Office how-tos (Excel, Word, PowerPoint), and Python error debugging covering ImportError, TypeError, AttributeError, ModuleNotFoundError, and JavaScript ReferenceError. Authored several of the site’s highest-traffic Excel and MS Office reference articles.

Expertise: JavaScript · MS Excel · MS Word · MS PowerPoint · Python · Python ImportError · Python TypeError · Python AttributeError · ModuleNotFoundError · JavaScript ReferenceError · Pygame  · View all posts by Glay Eliver →