IndexError: string index out of range

The error IndexError: string index out of range occurs when a character is retrieved from the index out side to the string range. A string is a sequence of character indexing from 0 to the number of characters in the string

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.

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 will start from 0 to the total number of characters in the string. If the index is out of range, the error “IndexError: string index out of range” is thrown.



Exception

The error “IndexError: string index out of range’” will be shown as below the stack trace.

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 using the character index. If the index is outside the index range of the string, the python interpreter can not locate the character from the memory location. Thus, an index error is thrown. The string index will start from 0 and end with the number of characters in the string.



Forward index of the string

Python supports two indexing types, forward indexing and backward indexing. The forward index will start at 0 and end 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 is printed in the same index sequence. The index value is increased to the next index value.

Index01234
Valueabcde


Backward index of the string

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

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


Solution 1

If the index value is out side of the index range of the string, the index error will be thrown. Make sure that the index range is with in the index range of the string. In the example below, the string contains 5 characters “hello”. The index value starts at 0 and ends at 4.

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

The string is dynamically created in real time. The string is iterated and the characters are retrieved based on the index. In that case , the value of the index is unpredictable. If the character in the string is retrieved by an index, 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 index value should be less than the total length of the string. Error 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]



Related Articles

Leave a Reply

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