Next / Previous / Contents / TCC Help System / NM Tech homepage

4.2. Indexing the positions in a sequence

Positions in a sequence refer to locations between the values. Positions are numbered from left to right starting at 0. You can also refer to positions in a sequence using negative numbers to count from right to left: position -1 is the position before the last element, position -2 is the position before the next-to-last element, and so on.

Here are all the positions of the string "ijklm".

To extract a single element from a sequence, use an expression of the form S[i], where S is a sequence, and i is an integer value that selects the element just after that position.

>>> s[0]
'a'
>>> s[4]
'e'
>>> s[5]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: string index out of range

The last line is an error; there is nothing after position 5 in string s.

>>> L[0]
0
>>> t[0]
'x'