Slicing (Python)

Slicing extracts a portion of a sequence (string, list, or tuple) using the [start:stop:step] notation. The start is included and the stop is excluded.

Python

Leaving out start or stop means 'from the beginning' or 'to the end.' A negative step, like [::-1], reverses the sequence.

Syntax

sequence[start:stop:step]

Examples

Python

letters = "python"
print(letters[0:3])
print(letters[::-1])

Output:

pyt
nohtyp

See also

Back to the Dictionary