mirror of https://github.com/python/cpython.git
introduce omitted index default before using it (GH-27775)
This commit is contained in:
parent
b1930bf75f
commit
599f5c8481
|
@ -269,14 +269,6 @@ to obtain individual characters, *slicing* allows you to obtain substring::
|
||||||
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
|
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
|
||||||
'tho'
|
'tho'
|
||||||
|
|
||||||
Note how the start is always included, and the end always excluded. This
|
|
||||||
makes sure that ``s[:i] + s[i:]`` is always equal to ``s``::
|
|
||||||
|
|
||||||
>>> word[:2] + word[2:]
|
|
||||||
'Python'
|
|
||||||
>>> word[:4] + word[4:]
|
|
||||||
'Python'
|
|
||||||
|
|
||||||
Slice indices have useful defaults; an omitted first index defaults to zero, an
|
Slice indices have useful defaults; an omitted first index defaults to zero, an
|
||||||
omitted second index defaults to the size of the string being sliced. ::
|
omitted second index defaults to the size of the string being sliced. ::
|
||||||
|
|
||||||
|
@ -287,6 +279,14 @@ omitted second index defaults to the size of the string being sliced. ::
|
||||||
>>> word[-2:] # characters from the second-last (included) to the end
|
>>> word[-2:] # characters from the second-last (included) to the end
|
||||||
'on'
|
'on'
|
||||||
|
|
||||||
|
Note how the start is always included, and the end always excluded. This
|
||||||
|
makes sure that ``s[:i] + s[i:]`` is always equal to ``s``::
|
||||||
|
|
||||||
|
>>> word[:2] + word[2:]
|
||||||
|
'Python'
|
||||||
|
>>> word[:4] + word[4:]
|
||||||
|
'Python'
|
||||||
|
|
||||||
One way to remember how slices work is to think of the indices as pointing
|
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.
|
*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
|
Then the right edge of the last character of a string of *n* characters has
|
||||||
|
|
Loading…
Reference in New Issue