Show Python string slicing with a start, a stop and a step, including negative indices.
A slice has the form text[start:stop:step]. The start is the first position that is included, the stop is the first position that is left out, and the step says how many positions you move between each one that you keep.
text = 'python'
print(text[0]) # p
print(text[0:3]) # pyt
print(text[2:]) # thon
print(text[:2]) # py
print(text[::2]) # pto
print(text[::-1]) # nohtyp
print(text[-2:]) # on
print(text[-3:-1]) # th
Read those results one at a time. text[0:3] takes the first three characters, so you get pyt. When you leave the stop out, the slice goes to the end of the string. When you leave the start out, the slice begins at position zero. A step of 2 takes every second character, which gives pto from this six-character word. A step of -1 walks backwards through the string, and that is the standard way to reverse it. Negative positions count from the end, so text[-2:] gives you the last two characters.
Two rules explain most of the mistakes people make with slices. The first rule is that the stop position is left out, which is different from the inclusive ranges that you may know from another language. The second rule is that a slice which reaches outside the string does not raise an error. text[0:100] on a six-character string returns the whole string, and text[10:20] returns an empty string. Only indexing with a single number that is out of range raises IndexError, which is exactly why a slice is safe to use on text whose length you are not sure about.
A negative step is also what makes the common slice idioms possible. text[::-1] is the reverse of the string. text[:n] is the first n characters. text[-n:] is the last n characters. And text[1:-1] removes the first and the last character. Each of those is a single operation, and none of them creates an intermediate copy beyond the result itself.