[docs] Bug in 3.1.2

Georg Brandl georg at python.org
Wed Jan 14 08:51:09 CET 2015


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 01/12/2015 11:56 PM, Navin Senguttuvan wrote:
> Hi , In the section 3.1.2 (strings) , string slice example (the last but
> one example code in the section) , shouldn't the second output be "Pytpy"
> 
> Thanks, Navin

Hi Navin,

I assume you refer to

>>> word = 'Python' 'J' + word[1:]
'Jython'
>>> word[:2] + 'py'
'Pypy'

The upper slice indices, like range() stop values, are exclusive: the
slice [0:2] will use indices 0 and 1.

This can also be expressed by this handy diagram, which appears s few
paragraphs before:

"""
One way to remember how slices work is to think of the indices as pointing
between characters, with the left edge of the first character numbered 0. Then
the right edge of the last character of a string of n characters has index n,
for example:

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
- -6  -5  -4  -3  -2  -1

"""

cheers,
Georg
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlS2H+0ACgkQN9GcIYhpnLAluQCfbOGAx/A2+HnPlG7RrEAE8myq
2LIAnirwEEyULG2QLDbGVeoS+fsrkorT
=lrJ6
-----END PGP SIGNATURE-----


More information about the docs mailing list