[Tutor] Python 101 at devshed.com confusion

Sak sak@nwlink.com
Thu, 28 Jun 2001 01:54:45 -0700


Hey everyone,

I'm just learning Python, and have been enjoying working through the Python 
101 articles on devshed.  I'm confused about something in part 2 of the 
Python 101 article, under the Sliced And Diced section...

http://www.devshed.com/Server_Side/Python/Python101_2/page6.html

At the beginning of the example, I'm told that the index of a string begins 
with 0 at the first character.  Where I get confused is right away when I 
extract a substring...

>>> str = "hobgoblin"
>>> str[3:9]
'goblin'
>>>

When I walk through this in my Python interpreter, I get an error when I try 
this...

>>> str = "hobgoblin"
>>> str[9]
Traceback (innermost last):
  File "<stdin>", line 1, in ?
IndexError: string index out of range
>>>

It makes perfect sense to me when using the len() function that I get a 
response that there are 9 characters in the string, but when I count through, 
starting at 0 for the first character, I only come up with eight for the last 
character too, just like the interpreter did.  So how come the first example, 
indexing [3:9] works?

Thanks,
-- 
Sak.