[Tutor] IndexError: string index out of range

Eri Mendz jerimed at myrealbox.com
Tue Nov 2 13:18:15 CET 2004


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.



More information about the Tutor mailing list