Skip to content Skip to sidebar Skip to footer

Python Saying String Index Is Out Of Range When I Verified That It Isn't

So I'm making this program to display the positions of a substring in a string. I have the tuples working properly now (I hope), but for some reason python's giving me an error say

Solution 1:

Indexing starts from 0, you need to use

elifn== len(word) andc== word[n - 1]:

Solution 2:

Arrays in Python are zero indexed. There fore if you have:

a = "Some String"
n = len(a)
a[n]

This is invalid, since the only valid indexes of a are [0:n-1]

Solution 3:

The error occurs because c == word[n] goes out of range.

Arrays are always indexed beginning with 0, therefore this should do the trick:

c == word[n - 1]

Post a Comment for "Python Saying String Index Is Out Of Range When I Verified That It Isn't"