[Tutor] Python 101 at devshed.com confusion

Bruce Sass bsass@freenet.edmonton.ab.ca
Thu, 28 Jun 2001 13:13:27 -0600 (MDT)


On Thu, 28 Jun 2001, Sak wrote:
<...>
> 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'
> >>>

ya got lotsa answers, but I think this is the most succinct way to
explain it...

given sequence: abcd

indexed:
         a   b   c   d
         |   |   |   |
         0   1   2   3
        -4  -3  -2  -1

sliced:
         a   b   c   d
       |   |   |   |   |
       0   1   2   3   4
      -4  -3  -2  -1

where a missing slice point defaults to just past the end, or just
before the beginning of the sequence.
i.e., "abcd"[-1:] == 'd' and "abcd"[:1] == 'a'


- Bruce