Skip to content

Python: How to Resolve "IndexError: string index out of range"

How to fix the Python IndexError: string index out of range by understanding string indexing, checking bounds with LBYL, or using EAFP with try-except blocks.

When working with strings in Python, the IndexError: string index out of range is a common runtime error. It occurs when you try to access a character using an index that does not exist within the string's boundaries. This happens because every character in a string has a specific position, or index, and your code is attempting to reference a position that is either before the start of the string or, more commonly, past its end.

This guide will explain Python's string indexing, show you how to reproduce the error, and provide the standard, robust methods for preventing it in your code.

Understanding String Indexing in Python

Python uses zero-based indexing, meaning the first character of a string is at index 0, the second is at index 1, and so on. The last character is always at the index equal to the length of the string minus one.

For a string my_str = "Hello":

  • The length is len(my_str), which is 5.
  • The valid indices range from 0 to 4.
Character 'H' 'e' 'l' 'l' 'o'
Index 0 1 2 3 4

Attempting to access any index outside of the 0 to 4 range will result in an IndexError.

Reproducing the IndexError

The error is triggered when you use an index that is greater than or equal to the string's length.

Example of code causing the error:

my_string = "Hello"

print(f"Character at index 4: {my_string[4]}")

print(my_string[5])

Output:

Character at index 4: o
Traceback (most recent call last):
  File "main.py", line 7, in <module>
    print(my_string[5])
IndexError: string index out of range

Solution 1: Check the Index Before Accessing (LBYL)

The most direct way to prevent the error is to check if the index is within the valid range before you try to access it. This is known as the LBYL ("Look Before You Leap") approach.

Solution:

my_str = "Hello"
index_to_check = 5

if index_to_check < len(my_str):
    print(f"Character at index {index_to_check}: {my_str[index_to_check]}")
else:
    print(f"Error: Index {index_to_check} is out of range for string of length {len(my_str)}.")

Output:

Error: Index 5 is out of range for string of length 5.

Solution 2: Use a try-except Block (EAFP)

An alternative, and often more Pythonic, approach is to attempt the operation and handle the error if it occurs. This is known as EAFP ("Easier to Ask for Forgiveness than Permission"). This pattern is useful when you expect the index to be valid most of the time.

Solution:

my_str = "Hello"
index_to_check = 99

try:
    character = my_str[index_to_check]
    print(f"Character at index {index_to_check}: {character}")
except IndexError:
    print(f"Error: Index {index_to_check} is out of range.")

Output:

Error: Index 99 is out of range.

Common Pitfall: The Off-by-One Error

A frequent cause of this IndexError is the "off-by-one" error, where a programmer mistakenly uses the length of the string as an index.

Example of common mistake:

my_str = "Hello"
length = len(my_str) # length is 5

try:
    last_char = my_str[length]
except IndexError as e:
    print(f"Error: {e}")

Output:

Error: string index out of range

The Correction: The correct index for the last character is always len(my_str) - 1.

my_str = "Hello"
length = len(my_str) # length is 5

last_char = my_str[length - 1]
print(f"The last character is: '{last_char}'")

Output:

The last character is: 'o'

Conclusion

The IndexError: string index out of range is a fundamental error that signals an attempt to access a non-existent position in a string. To prevent it:

Approach When to Use Example
Check Index (LBYL) When it's common for an index to be out of bounds and you need to handle that case explicitly. if n < len(s): ...
try-except (EAFP) When you expect the index to be valid most of the time and an out-of-bounds access is an exceptional case. try: s[n] except IndexError: ...

By always remembering the zero-based indexing rule and that the maximum valid index is len(string) - 1, you can write robust code that avoids this common error.

Navigation