Last modified: Dec 06, 2025 By Alexander Williams
Fix TypeError: '<' Not Supported Between Str and Int
You see a TypeError in Python. It says you cannot compare a string and an integer. This is a common error for beginners. It happens when you use comparison operators like <, >, <=, or >=. The error occurs because Python cannot decide if a word is less than a number. It needs the same data type for a fair comparison.
This guide will explain why this error happens. You will learn how to fix it. We will show you clear code examples. You will also learn best practices to avoid it in the future.
Understanding the TypeError
Python is a strongly typed language. This means it is strict about data types. You cannot directly compare different types. The comparison operator < is used to check if one value is less than another.
It works fine when both values are integers or both are strings. But it fails when you mix a string and an integer. Python does not guess your intention. It raises a TypeError to stop the operation.
# This will cause the TypeError
age = input("Enter your age: ") # User types '25', but it's a string
if age < 18:
print("You are a minor.")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'int'
The input() function always returns a string. The variable `age` holds the string "25", not the number 25. The number 18 is an integer. The comparison `age < 18` tries to compare a string to an integer. This causes the error.
How to Fix the Error
The solution is to convert one of the values. You must make sure both sides of the comparison are the same type. Usually, you convert the string to an integer. Use the int() function for this conversion.
Solution 1: Convert String to Integer with int()
Use the int() built-in function. It converts a string that looks like a number into an integer. This is the most common fix for this error.
# Fixed by converting the string to an integer
age_input = input("Enter your age: ")
age = int(age_input) # Convert the string to an integer
if age < 18:
print("You are a minor.")
else:
print("You are an adult.")
Enter your age: 16
You are a minor.
Important: The int() function will fail if the string is not a valid integer. For example, "twenty" or "25.5" will cause a ValueError. You should handle this with a try-except block.
Solution 2: Handle Invalid Input Gracefully
Always validate user input. Wrap your conversion in a try-except block. This prevents your program from crashing on bad input.
# Safe conversion with error handling
try:
user_input = input("Enter a number: ")
number = int(user_input)
if number < 10:
print("Your number is less than 10.")
else:
print("Your number is 10 or greater.")
except ValueError:
print("That was not a valid integer. Please enter a whole number.")
Solution 3: Convert Integer to String (Less Common)
Sometimes you might want to compare strings. You can convert the integer to a string using str(). This compares lexicographically (alphabetical order), not numerically.
# Converting integer to string for string comparison
my_string = "5"
my_integer_as_string = str(10) # Convert integer 10 to string "10"
if my_string < my_integer_as_string:
print("String '5' is less than string '10' in lexical order.")
String '5' is less than string '10' in lexical order.
Be careful! String "10" is less than string "5" because '1' comes before '5' in character order. This is usually not what you want for numbers.
Common Scenarios and Examples
This error pops up in many places. Here are a few common situations where you might encounter it.
Scenario 1: Reading from Files or JSON
Data from files, APIs, or JSON is often read as strings. You must convert it before numerical comparison. For JSON issues, see our guide on Fix TypeError: object not JSON serializable.
# Data from an external source is often a string
data_from_json = {"score": "95"} # The value is a string
score_value = int(data_from_json["score"]) # Convert it
if score_value > 90:
print("Grade A")
Scenario 2: In Loops and List Comprehensions
You might have a list of mixed types or strings that represent numbers. You need to clean the data first.
# A list with string numbers
string_numbers = ["5", "12", "3", "20"]
# Convert all items to integers for comparison
int_numbers = [int(x) for x in string_numbers]
# Now find the maximum value
print(max(int_numbers))
20
Scenario 3: With Data Structures like Dictionaries
Dictionary values can be of any type. Ensure you know the type before comparing. For other dict errors, learn about Fix TypeError: unhashable type 'dict' in Python.
Best Practices to Avoid the Error
Follow these tips to write cleaner code and prevent this error.
1. Know Your Data Types: Use the type() function to check a variable's type when debugging.
2. Convert Early: Convert user input or external data to the correct type as soon as you read it.
3. Use Consistent Types: Design your data flow to keep types consistent. Avoid mixing strings and integers in the same list for numerical work.
4. Add Type Hints: Python's type hints can help you document what type a variable should be. Tools like mypy can catch potential type mismatches before you run the code.
Related TypeErrors in Python
Python has several common TypeErrors related to type mismatches. Understanding this one helps with others. For instance, you might see Fix Python TypeError: Can't Concatenate List and Int when using the `+` operator incorrectly. Another frequent error is Fix TypeError: a bytes-like object is required, not 'str' when working with binary data.
Conclusion
The "TypeError: '<' not supported between instances of 'str' and 'int'" is a fundamental Python error. It teaches you about Python's type system. The fix is simple: convert your data to compatible types using int() or str().
Always remember to validate input and handle conversion errors. This makes your programs robust and user-friendly. By understanding this error, you build a stronger foundation for debugging more complex issues in Python.