Paul's Python CoursePPC

Slicing strings

Not met yet

A string is a row of characters, each with a position counted from 0: name[0] is the first character, name[-1] the last. A slice takes part of it: name[:3] is the first three, name[-4:] the last four, and the slice stops just before its second number. Slicing text gives text. Any single position past the end is an IndexError; a slice past the end just stops at the end.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks the first character of a string is at position 1.
  • Thinks items[1:3] includes the item at position 3.
  • Thinks slice positions count from 1, so items[1:] starts at the first item.

Where it comes up

Slicing a Python string means taking part of it by position. name[0] gets one character (an index), and name[2:5] gets a run of characters (a slice), which is how Python does what other languages call a substring. Positions start at 0, and negative positions count back from the end, so name[-1] is the last character. You use it to pick out initials, keep the last four digits of a number, drop a prefix, or reverse a word. A slice of a string is always another string, and the original is left as it was.

How it works

A slice can take three numbers, start:stop:step, and each one is optional. Leave out start and it begins at the beginning; leave out stop and it runs to the end; the step says how far to move each time.

word = "Clockwork"
print(word[2:5])
print(word[-4:])
print(word[::2])
print(word[::-1])
print(word[5:2:-1])
ock
work
Cokok
krowkcolC
wkc

word[2:5] takes positions 2, 3 and 4: it stops before 5, so it holds stop - start characters (as long as both are inside the string). word[::2] takes every second character. A negative step walks backwards, and [::-1] with no start or stop walks the whole string from the end, which is the usual way to reverse text. With a negative step, start has to be the larger number: word[5:2:-1] begins at position 5 and works down to position 3.

Slices and single positions behave differently at the edges:

code = "SM42"
print(code[2:10])
print(code[10:] == "")
print(code[4])
42
True
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    print(code[4])
          ~~~~^^^
IndexError: string index out of range

A slice asks for whatever is in a range, and stops quietly at the end; a range with nothing in it gives the empty string "". A single index asks for one particular character. "SM42" has positions 0 to 3, so there is nothing at 4 to hand back, and Python raises an IndexError instead of guessing.

Common mistakes

  • Counting from 1. The first character is at position 0, so "SM42"[1] is "M", and the last is at len(code) - 1, or more simply -1. The same applies to slices: name[1:] starts at the second character and drops the first. To get the first three, write name[:3].
  • Expecting the stop position to be included. "python"[1:3] is "yt", positions 1 and 2 only. One way to picture it: the numbers mark the gaps between characters, and the slice takes what lies between two gaps.
  • Trying to change a character through an index. name[0] = "B" is a TypeError, because strings can't be changed. Build a new one with a slice instead: name = "B" + name[1:].

Related

Slicing works the same way on lists, covered in slicing, and negative indices explains counting from the end. For why a string can't be edited in place, see Strings can't be changed.