IndexError: string index out of range

The python error IndexError: string index out of range occurs if a character is not available at the string index. The string index value is out of range of the String length. The python error IndexError: string index out of range occurs when a character is retrieved from the out side index of the string range.

The IndexError: string index out of range error occurs when attempting to access a character using the index outside the string index range. To identify a character in the string, the string index is used. This error happens when access is outside of the string’s index range.

If the character is retrieved by an index that is outside the range of the string index value, the python interpreter can not locate the location of the memory. The string index starts from 0 to the total number of characters in the string. If the index is out of range, It throws the error IndexError: string index out of range.

A string is a sequence of characters. The characters are retrieved by the index. The index is a location identifier for the ordered memory location where character is stored. A string index starts from 0 to the total number of characters in the string.



Exception

The IndexError: string index out of range error will appear as in the stack trace below. Stack trace shows the line the error has occurred at.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    print ("the value is ", x[6])
IndexError: string index out of range
[Finished in 0.1s with exit code 1]


Root cause

The character in the string is retrieved via the character index. If the index is outside the range of the string index the python interpreter can’t find the character from the location of the memory. Thus it throws an error on the index. The string index starts from 0 and ends with the character number in the string.



Forward index of the string

Python allows two forms of indexing, forward indexing and backward indexing. The forward index begins at 0 and terminates with the number of characters in the string. The forward index is used to iterate a character in the forward direction. The character in the string will be written in the same index order.

Index01234
Valueabcde


Backward index of the string

Python allows backward indexing. The reverse index starts from-1 and ends with the negative value of the number of characters in the string. The backward index is used to iterate the characters in the opposite direction. In the reverse sequence of the index, the character in the string is printed. The back index is as shown below

Index-5-4-3-2-1
Valueabcde


Solution 1

The index value should be within the range of String. If the index value is out side the string index range, the index error will be thrown. Make sure that the index range is with in the index range of the string. 

The string index range starts with 0 and ends with the number of characters in the string. The reverse string index starts with -1 and ends with negative value of number of characters in the string.

In the example below, the string contains 5 characters “hello”. The index value starts at 0 and ends at 4. The reverse index starts at -1 and ends at -5.

Program

x = "hello"
print "the value is ", x[5]

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    print "the value is ", x[5]
IndexError: string index out of range
[Finished in 0.0s with exit code 1]

Solution

x = "hello"
print "the value is ", x[4]

Output

the value is  o
[Finished in 0.1s]


Solution 2

If the string is created dynamically, the string length is unknown. The string is iterated and the characters are retrieved based on the index. In this case , the value of the index is unpredictable. If an index is used to retrieve the character in the string, the index value should be validated with the length of the string.

The len() function in the string returns the total length of the string. The value of the index should be less than the total length of the string. The error IndexError: string index out of range will be thrown if the index value exceeds the number of characters in the string

Program

x = "hello"
index = 5
print "the value is ", x[index]

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print "the value is ", x[index]
IndexError: string index out of range
[Finished in 0.1s with exit code 1]

Solution

x = "hello"
index = 4
if index < len(x) :
	print "the value is ", x[index]

Output

the value is  o
[Finished in 0.1s]


Solution 3

Alternatively, the IndexError: string index out of range error is handled using exception handling. The try block is used to handle if there is an index out of range error.

x = "hello"
print "the value is ", x[5]

Exception

the value is 
Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    print "the value is ", x[5]
IndexError: string index out of range
[Finished in 0.1s with exit code 1]

Solution

try:
	x = "hello"
	print "the value is ", x[5]
except:
	print "not available"

Output

the value is  not available
[Finished in 0.1s]



Related Articles

Leave a Reply

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