TypeError: 'str' object cannot be interpreted as an integer (Fix)

Fix the Python error TypeError: 'str' object cannot be interpreted as an integer. This page explains what the error means, common causes, and the fastest ways to correct your code.

Quick fix #

text = "5"
number = int(text)

for i in range(number):
    print(i)

This error happens when Python expects an integer, but you give it a string instead. Convert the string first if it contains a number.

What this error means #

Python is telling you that:

  • It expected an integer
  • It received a string
  • It cannot use that string where a whole number is required

This often appears when working with:

  • range()
  • list indexes or slices
  • repetition operations
  • functions that require integer arguments

Why this error happens #

This error usually happens because a value looks like a number, but is actually text.

Common reasons:

  • input() always returns a string
  • Numbers read from files or APIs may arrive as strings
  • Quotes around a number make it a string, not an integer
  • A variable may contain text when you think it contains a number

For example:

count = "5"   # string
print(type(count))

Output:

<class 'str'>

Even though "5" looks numeric, Python treats it as text.

Common places where it appears #

You will often see this error in code like this:

  • range("5") instead of range(5)
  • using a string as a list index
  • passing a string to a function that needs an integer
  • using user input directly in a loop

Example:

items = ["a", "b", "c"]
index = "1"

print(items[index])

This fails because list indexes must be integers, not strings.

Example that causes the error #

A very common case is using input() directly inside range().

count = input("How many times? ")

for i in range(count):
    print(i)

Python raises the error because count is a string.

Traceback (most recent call last):File "example.py", line 3, in <module>for i in range(count):TypeError: 'str' object cannot be interpreted as an integerWhere it happened — file and lineWhat went wrong — the exception typeWhy — the detailed message
Read it bottom-up: a string was used where range() needs an integer, on the for loop line.

If the user types 5, the value is still "5", not 5.

How to fix it #

The usual fix is to convert the string with int() before using it.

count = int(input("How many times? "))

for i in range(count):
    print(i)

Output if the user enters 5:

0
1
2
3
4

Another example #

text = "10"
number = int(text)

print(number + 2)

Output:

12

Good things to check #

  • Convert the value with int()
  • Check the type with type()
  • Make sure the string contains a valid whole number
  • Validate input before conversion

If you need more help with conversion, see how to convert a string to int in Python.

If int() also fails #

Sometimes you fix one problem and get a new one:

text = "3.5"
number = int(text)

This raises a ValueError, because "3.5" is not a valid integer string.

Other examples that fail with int():

  • "3.5"
  • "ten"
  • ""

If decimals are allowed, use float() instead. If not, clean and validate the input first.

Related page: ValueError: invalid literal for int() with base 10

Debugging steps #

If you are not sure where the problem is coming from, check the value before the line that fails.

Useful debugging commands:

print(value)
print(type(value))
print(repr(value))
print(isinstance(value, int))

What each one helps you see #

  • print(value) shows the current value
  • print(type(value)) shows whether it is a string or integer
  • print(repr(value)) helps reveal spaces or hidden characters
  • isinstance(value, int) checks whether the value is an integer

Example debugging session #

value = input("Enter a number: ")

print(value)
print(type(value))
print(repr(value))
print(isinstance(value, int))

for i in range(value):
    print(i)

If the user enters 5, the debug output will show that the value is still a string.

Common mistakes #

These are some of the most common causes of this error:

  • Using input() directly inside range()
  • Storing numbers as quoted strings
  • Reading numeric text from a file and not converting it
  • Passing a string index to a list or tuple operation
  • Using command-line arguments without conversion

Also watch for cases where a variable is reassigned:

count = 5
count = "5"

for i in range(count):
    print(i)

The second assignment changes count from an integer to a string.

FAQ #

Why does input() cause this error so often? #

Because input() always returns a string. You usually need int(input(...)) when asking for a whole number.

What is the difference between a string and an integer? #

A string is text, even if it looks like a number. An integer is a whole number value Python can use in numeric operations.

Should I use int() or float()? #

Use int() for whole numbers like 5. Use float() for decimal numbers like 5.2.

Why does "5" not work the same as 5? #

Because "5" is text inside quotes. Python treats it as a string, not a number.

Can I fix this with type checking first? #

Yes. You can inspect the value with type() or isinstance() before using it where an integer is required.

See also #

Press Esc to close