TypeError: not supported between instances of ‘str’ and ‘int’

If an incompatible combination of operands is used with an operator, the TypeError: <operator> not supported between instances of ‘str’ and ‘int’ exception or error message will be generated. Imagine using a > or greater than operator acting on str ‘4’ and int 2 in an if statement like if ‘4’ > 2. Python will tell us there is a TypeError in that operation. The > operator only supports numeric data. This error is frequently seen among those who are new to Python. In this article, we will be going over this exception and learning how to avoid it.

1.  What is the problem?

Let us reproduce this error message by creating instances of str and int objects and using a comparison operator > or greater than.

code

# Create instances of str and int data types.
var1 = str(10)
var2 = int(20)
  
# Check if var1 is greater than var2.
if var1 > var2:
    print('var1 is greater than var2')

output

Traceback (most recent call last):
  File "F:\Project\kodlogs\a1.py", line 6, in <module>
    if var1 > var2:
TypeError: '>' not supported between instances of 'str' and 'int'

CautionTypeError: ‘>’ not supported between instances of ‘str’ and ‘int’

From a programmer’s perspective, this is understandable as we cannot compare if str ’10’ is greater than int 20 because they are of different data types. One is str and the other is an int. The var1 and var2 are what we call the operands.

In total, there are 4 comparison operators that have similar error messages. They are: >, >=, <, <=. If we change the operator from > to < we will get the following.

TypeError: '<' not supported between instances of 'str' and 'int'

1.1.  Comparison Operations: Example problem

Create a program that will prompt users to input their age in years. Print “minor” if it is below 18 otherwise print “adult”.

code

age = input('How old are you? ')
if age < 18:
    print('minor')
else:
    print('adult')

output

How old are you? 15
Traceback (most recent call last):
  File "F:\Project\kodlogs\a1.py", line 2, in <module>
    if age < 18:
TypeError: '<' not supported between instances of 'str' and 'int'

We got a TypeError because the age is of type str, not an int. We know that a comparison operator such as < cannot operate if one variable is int and the other is str.

FAQQ: How do we know the type of a variable or object?
A: Use the built-in function type() to get the type of an object.

1.2.  Arithmetic Operations: Example problem

When we try to perform arithmetic operations between integer and string also this issue can appear.

Full Name = “Robert Smith”
Grade= 9
output = Name + Grade# This will show the TypeError message in the output

The cause of the error is the return type of the built-in function input. This function returns a str type.

Solution:

2.  How to avoid this error?

Well we know that the comparison operators such as <, <=, >, and >= can only function if the operands are of numeric types. So if we know in advance that the variables to be operated upon are dissimilar like str and int then we will not use those operators.

2.1.  Solution to an example problem

In cases where the type is str, we can convert it to int if it is a number, similar to the problem in section 1.1. Python has a built-in function int() that converts a string number to int.

code

age = input('How old are you? ')
  
# Before comparing to 18 convert it to integer.
age = int(age)

if age < 18:
    print('minor')
else:
    print('adult')

output

How old are you? 15
minor

So we did not get the error message because we converted the age to an integer. Even ChatGPT knew that the output type of input() is a string.

Conversation with ChatGPT
The box with F is me.

image

3.  Conclusion #

We will encounter the exception message TypeError: <operator> not supported between instances of ‘str’ and ‘int’ if we try to use comparison operators such as >, >=, <, <= to operate on dissimilar operands such as str and int. To prevent this issue from arising, it is important to be aware of the data type that is being manipulated. These operators do not work with a combination of string and integer data types and are only compatible with numeric data types.