Indexing is the way to identify a particular
item in a sequence. R[3]
identifies
the fourth item of sequence R (remember, indexing
starts at 0). What about identifying a block of
items in a sequence? R[2:5]
is the
sequence starting with the third and going up to
the sixth item of R. Slicing notation (with one
colon) applied to a list or tuple always identifies
a sequence (list or tuple), even if the length is
0 or 1. Slicing does not refer to an individual
item the way that indexing does. For strings,
slicing works differently: a slice of a string is
always a string, but so is an indexed value in a
string. Examples follow, including mixed examples
that show indexing and slicing, and taking slices
of slices.
Text = "certificate of product ratings" |
a = Text[3:14] |
b = Text[:12] |
c = Text[:-18] |
d = Text[Text.index("f"):len(Text)] |
e = Text[10:10+5] |
f = Text[13:13] |
Items = [4,1,2,3,1,9,7,0,6] |
g = Items[2:-2] |
h = Items[2:9] |
i = h[2:4] |
j = Items[2:9][2:4] |
k = h[1] |
l = Items[2:9][1] |