Python TypeError: ‘str’ object cannot be interpreted as an integer Solution

By | November 17, 2021
Python TypeError ‘str’ object cannot be interpreted as an integer Solution

The Python range method can only accept integer values as arguments. All the three arguments it accepts must be of int data type, and if we try to pass a string number value, we will receive the error “TypeError: ‘str’ object cannot be interpreted as an integer”.

This Python guide discuss “TypeError: 'str' object cannot be interpreted as an integer” in detail. It also discusses a common example scenario where you may encounter this error in your program. By the end of this tutorial, you will have a complete idea about how to fix this error in Python.

Vamware

Python issue: TypeError: ‘str’ object cannot be interpreted as an integer

With the help of the range() function, we can create a list-like object of integer numbers. The range() function is generally used with for loop when we want to iterate over a list using the list index number.

The range function can accept 3 arguments, start, end and steps. And all these three need to be an integer value of int data type.

Example

for even in range(0,20,2):
    print(even)

Output

0
2
4
6
8
10
12
14
16
18

In this example 0, 20  and 2 all three are the integer number with int data type, and if we try to pass the same number with string data type, Python will raise the error TypeError: 'str' object cannot be interpreted as an integer.

Error Example

for even in range(0,'20',2):
    print(even)

Output

Traceback (most recent call last):
  File "main.py", line 1, in 
    for even in range(0,'20',2):
TypeError: 'str' object cannot be interpreted as an integer

In this example, we received the error because the second argument in the range(0, '20', 2) function is a string.

And by reading the error statement TypeError: 'str' object cannot be interpreted as an integer we can conclude why Python raised this error.

Like a standard error statement, this error also has two sub-statements.

  1. TypeError is the Exception Type
  2. ‘str’ object cannot be interpreted as an integer is the error message.

We are receiving the TypeError exception becaue the range function was expecting an int data type value and we passed a string.

And the error message “‘str’ object cannot be interpreted as an integer” is clearly telling us that Python could not use the string data because it was expecting an integer.

Common Example Scenario

Now let’s discuss a common case where you may encounter the following error in your Python program.

Let’s say we need to write a program that prints all the prime number between 1 t0 n. Where n is the last number of the series.

Error Example

#function that check if the number is a prime number or not
def is_prime(num):
    for i in range(2,num):
        if num % i ==0:
            return False
    return True

#input the number upto which we want to find the prime numbers
n = input("Enter the last number: ")
print(f"Prime numbers upto {n} are:") 
for i in range(2,n):
    if is_prime(i):
        print(i)
        

Output

Enter the last number: 12
Prime numbers upto 12 are:
Traceback (most recent call last):
  File "main.py", line 12, in 
    for i in range(2,n):
TypeError: 'str' object cannot be interpreted as an integer

Break the error 

After reading the error statement we can tell that there is something wrong with the statement for i in range(2,n). And by reading the Error message, we can easily conclude that the value of n in string is not an integer.

Solution

Whenever we accept input from the user, it is always stored in string data type. If we want to pass that input data into a function like range() we first need to convert into integer using the int() function.

#function that check if the number is a prime number or not
def is_prime(num):
    for i in range(2,num):
        if num % i ==0:
            return False
    return True


#input the number upto which we want to find the prime numbers
#and convert it into integer
n = int(input("Enter the last number: "))

print(f"Prime numbers upto {n} are:")
for i in range(2,n+1):
    if is_prime(i):
        print(i)
        

Output

Enter the last number: 12
Prime numbers upto 12 are:
2
3
5
7
11

Now our code runs smoothly without any error.

Conclusion

The Error “TypeError: ‘str’ object cannot be interpreted as an integer” is very common when we deal with range functions and identifers. This error occurs in a Python program when we pass a string number or value to the range function, instead of integers. The most common case is when we input the data from the user and use the same data with the range function without converting it into an integer.

If you are still getting this error in your Python program, feel free to send your query and code in the comment section. We will try to help you as soon as possible.

People are also reading:

Leave a Reply

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