newbie string question

Peter Hansen peter at engcorp.com
Thu Jun 20 23:21:27 EDT 2002


Don Low wrote:
> 
> There's probably a very simple explanation, but I can't see it for now, so
> bear with me.
> 
> >>>pystr = 'Python'
> >>>pystr = [5]
> 'n'
> >>>pystr [2:5]
> 'tho'
> 
> The question is, why isn't pystr [2:5] 'thon' instead of 'tho'. Could
> somebody explain?

The numbers don't represent the position of the items in the sequence,
but the places *between* items where you would "slice" the sequence
to get the subsequence.  That's why it's called a slice operation.

Picture it this way:

   0   1   2   3   4   5    <-- subscript index
 +---+---+---+---+---+---+
"| P | y | t | h | o | n |"
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6  <-- slice index

Your slice went from 2 to 5, so it got only the "tho" part of the string.
Here's what you got:

 +---+---+---+
 | t | h | o |
 +---+---+---+
 2   3   4   5

Other languages confuse the indices used when slicing things (as in
the common substring() method) and when subscripting things (when you 
take just a single item, such as pystr[4] which is "o" in the above).

Python avoids up this confusion by realizing they are not the same
thing.  (Of course, this can confuse newcomers who don't realize 
what happened, but at least the pain happens for you only once. :-)

-Peter



More information about the Python-list mailing list