Index
The index in Python is from 0 the beginning, so the index 0 corresponds to the first element of the sequence 1 . To get 5 the first element, you need to use the index value 4 .
In addition to the forward index,Python introduces the use of negative index values, which are counted from the back forward, for example, -2 an index that represents the penultimate 2 element.
An error occurs when a single index is greater than or equal to the length of the string.
Sharding
Shards are used to extract the desired subsequence from a sequence, using:
var[lower:upper:step]
Its scope includes lower , but does not include upper , that [lower, upper) represents the step value interval size, if there is no default 1 .
s = ' Hello World ' s[1:3] ' el ' s[1:-2] ' ello wor ' s[:3] ' hel ' s[-3:] ' rld ' s[::2] ' hlowrd '
When the value of step is negative, omitting lower means that the Shard begins at the end, and omitting upper means always slicing to the beginning.
S[::-1] ' Dlrow Olleh '
Python----------indexes and shards