Online Tutorials & Training Materials | STechies.com
Register Login

How to Fix Error String Index Out of Range Python

|| 0

How to Fix Error String Index Out of Range Python
Stechies

In python “String index out of range” error occurs when we try to access un-defind element from the list.

Example: 

# Declaring list
list_fruits = ['apple', 'banana', 'orange']
# Print value of list at index 3
print(list_fruits[3]);

Output:

Traceback (most recent call last):
  File "list-index.py", line 2, in <module>
    print(list_fruits[3]);
IndexError: list index out of range

In the above example we have created a list name “list_fruits” with three values apple, banana and orange. Here we are trying to print index 3.

It is obvious that the index list starts from 0, that’s why in the list the last index is 2 not 3.

Correct Example: 

# Declaring list
list_fruits = ['Apple', 'Banana', 'Orange']
# Print list element at index 2
print(list_fruits[2]);

Output: 

Orange

String index out of range python while

Example with While loop

# Declaring list
list_fruits = ['Apple', 'Banana', 'Orange']

i=0
# while loop less then and equal to list "list_fruits" length.
while i <= len(list_fruits):
    print(list_fruits[i])
i += 1

Output:

Apple
Banana
Orange
Traceback (most recent call last):
  File "list-index-while.py", line 5, in <module>
    print(list_fruits[i])
IndexError: list index out of range

In the above example error occur in line 5 as shown in output where print(list_fruits[i]), means that value of “i” exceeds to the index value of list “list_fruits”.

If you need to check why this error occurs, just print the value of “i” just before “print(list_fruits[i])” statement.

print(i)

Example

# declaring list
list_fruits = ['Apple', 'Banana', 'Orange']

i=0
# while loop less then and equal to list "list_fruits" length.
while i <= len(list_fruits):
    # print the value of i
    print(i)
    # print value of list
    print(list_fruits[i])
    i += 1

Output:

0
Apple
1
Banana
2
Orange
3
Traceback (most recent call last):
  File "list-index-while.py", line 9, in <module>
    print(list_fruits[i])
IndexError: list index out of rang

In the above example output we can see that the value of ‘i’ goes to “3”, where as our list index is only up to 2.

Solution for this error 

# declaring list
list_fruits = ['Apple', 'Banana', 'Orange']

i=0
# while loop less then list "list_fruits" length
while i < len(list_fruits):
    # print the value of i
    print(i)
    # print value of list
    print(list_fruits[i])
    i += 1

Output:

0
Apple
1
Banana
2
Orange

List index out of range python for loop

Example:

# declaring list

list_fruits = ['Apple', 'Banana', 'Orange']

# for loop to print the index from 0 to 3

for i in range(0,4):
    # print the value of i
    print(i)
    # print value of list
print(list_fruits[i])

Output:

0
Apple
1
Banana
2
Orange
3
Traceback (most recent call last):
  File "list-index-for.py", line 9, in <module>
    print(list_fruits[i])
IndexError: list index out of range

In the above example we are printing the value at index 3 but out list have index only up to 2.

To avoid such type of error we have to run for loop in the range of “list” length.

Solution:

# declaring list
list_fruits = ['Apple', 'Banana', 'Orange']

# for loop to print the index in the range of list length
for i in range(len(list_fruits)):
    # print the value of i
    print(i)
    # print value of list
print(list_fruits[i])

Output:

0
Apple
1
Banana
2
Orange