[Tutor] IndexError: string index out of range

Liam Clarke cyresse at gmail.com
Tue Nov 2 14:07:06 CET 2004


Hi Eri, 

I started learning 4 weeks ago, so good luck. Python is great.

I'm gonig to do what I do nearly every reply, and plug Alan Gauld's tutorial.

http://www.freenetpages.co.uk/hp/alan.gauld/tutor2/index.htm

Check it out, it's quite indepth as to specifics and quirks. The
tutorial you're doing is good also, but I find that while it teaches
good method, it's a little sparse in explaining how to implement that
method, or why it is implemented that way.

So yeah, the above tutorial is really good. I used it, and I'm halfway
my first GUI app, so it helps. : )

Now, onto your problem.

Index error means that for say, a five value index, a=[1,2,3,4,5] you
tried to access the 6th value or more.

This is what's getting you - 

index = -1
> >>> while index < len(fruit):

[-1]    a
> [-2]    n
> [-3]    a
> [-4]    n
> [-5]    a
> [-6]    b
> [-7]    Traceback (most recent call last):
>   File "<stdin>", line 2, in ?
> IndexError: string index out of range

Ok, so the length of fruit is 6. 

-1 to -99999999999999999999999999999 is always going to be less than 6.

So yeah, while index < len(fruit) becomes while (-1, -2, -3.. -999) < 6:

Your while condition is always true. I would change it to 

lengthFruit = len(fruit) (Just to make the below examples easier to read)

while index > -lengthFruit

OK, so while -1 to -6 is greater than -6, do the following. On the 6th
loop through, it will do the last slice and then subtract 1 from
index, at which point index will become -6, and your loop will end.

or 

while index != -(lengthFruit+1)

This one would work as well, != means 'does not equal' So while index
does not equal -7, do the following.

Personally, I think that the way you're indexing in reverse is a bit
awkward and confusing(Unless you're counting position from line end or
something.)

The computer always counts from 0 up, and I generally tend to stick
with convention unless I've got a real good reason to blaze my own
trail.


So, another way would be - 

>>>for index in range(lengthFruit-1, -1, -1):
...                           print [ndex], '\t', fruit[index]


Have you used a for loop before? So basically, 

for x in range(10) will increment x from 0 to 9 each loop.  for x in
range(5,10) will increment from 5 to 9. for x in range(0, 10, 2):
print x would produce 0, 2, 4, 6, 8, it's counting by 2.

But range defaults to is range(0(start), yourValue(stop), 1(step)) so
when you tell it range(10) it counts from 0 to 9, 1 at a time.

Notice how it counts up to 10, but not including it? This works well
with a list. List x has an index of x[0] to x[9], ten values, so
len(x) will be 10. And if you did

q=0
while q != len(x):
    print x[q]
    q= q + 1  (a short hand way to write this is ' q += 1'  subtract use -=)

You would get an IndexError, because x[len(x)] which is x[10] doesn't
exist. x goes from x[0] to x[9].

Anyway, digression over, 

So this statement 'for index in range(lengthFruit-1, -1, -1):'

says start at 5, count to, but not including -1, and count by -1 each
loop. lengthFruit for 'banana' will be 6, but x[5] is the last letter
of 'banana' And we count to -1, to ensure that fruit[0] is printed
also.

So you would get
>>>for index in range(lengthFruit-1, -1, -1):
...                           print [index], '\t', fruit[index]

[5]   a
[4]   n
[3]   a
[2]   n
[1]   a
[0]   b

which is an upside down version of yours

> [0]     b
> [1]     a
> [2]     n
> [3]     a
> [4]     n
> [5]     a


I hope my long winded reply helps, and now my standard disclaimer -

There is no doubt an easier, more elegant way to do it, and someone
more knowledgeable than me will post it shortly. But, my way works.


Regards,

Liam Clarke

On Tue, 02 Nov 2004 15:18:15 +0300, Eri Mendz <jerimed at myrealbox.com> wrote:
> 
> Hello all,
> 
> I'm geting IndexError when iterating over a string in reverse. What
> exactly does this mean? help(IndexError) in the interpreter isnt very
> helpful. I'm a beginner trying to follow examples in the online tutorial
> How To Think Like Computer Scientist (Python).
> 
> I understand slicing of strings, lists and tuples but a bit confused
> with indexing. Kindly enlighten me on this part. Fortunately i have the
> online tutorials printed out for reference but i agree the best way to
> learn is to practice and practice coding. Time is liability on my side
> due to my day job but im trying to squeeze time in the night for python
> the best i can.
> 
> My goal of learning python is to learn it to help with system
> administration of Linux/Windows. Maybe it can be of help to me one day.
> 
> >>> fruit = 'banana'
> >>> index = 0
> >>> while index < len(fruit):
> ...     print [index], '\t', fruit[index]
> ...     index = index + 1
> ...
> [0]     b
> [1]     a
> [2]     n
> [3]     a
> [4]     n
> [5]     a
> >>>
> >>> index = -1
> >>> while index < len(fruit):
> ...     print [index], '\t', fruit[index]
> ...     index = index - 1
> ...
> [-1]    a
> [-2]    n
> [-3]    a
> [-4]    n
> [-5]    a
> [-6]    b
> [-7]    Traceback (most recent call last):
>   File "<stdin>", line 2, in ?
> IndexError: string index out of range
> 
> --
> Regards,
> erimendz
> 
> --
> This message was sent with an unlicensed evaluation version of
> Novell NetMail. Please see http://www.netmail.com/ for details.
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list