mirror of
https://github.com/python/cpython
synced 2024-10-09 02:17:31 +00:00
SF bug #699237: Tutorial uses omitted slice indices before explaining them
Moved up the explanation of slice default arguments.
This commit is contained in:
parent
83245b5828
commit
60de2e837f
|
@ -640,6 +640,17 @@ separated by a colon.
|
|||
'lp'
|
||||
\end{verbatim}
|
||||
|
||||
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.
|
||||
|
||||
\begin{verbatim}
|
||||
>>> word[:2] # The first two characters
|
||||
'He'
|
||||
>>> word[2:] # All but the first two characters
|
||||
'lpA'
|
||||
\end{verbatim}
|
||||
|
||||
Unlike a C string, Python strings cannot be changed. Assigning to an
|
||||
indexed position in the string results in an error:
|
||||
|
||||
|
@ -664,17 +675,6 @@ efficient:
|
|||
'SplatA'
|
||||
\end{verbatim}
|
||||
|
||||
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.
|
||||
|
||||
\begin{verbatim}
|
||||
>>> word[:2] # The first two characters
|
||||
'He'
|
||||
>>> word[2:] # All but the first two characters
|
||||
'lpA'
|
||||
\end{verbatim}
|
||||
|
||||
Here's a useful invariant of slice operations:
|
||||
\code{s[:i] + s[i:]} equals \code{s}.
|
||||
|
||||
|
|
Loading…
Reference in a new issue