9.7. The Slice Operator¶
A substring of a string is called a slice. Selecting a slice is similar to selecting a character:
singers = "Peter, Paul, and Mary"
print(singers[0:5])
print(singers[7:11])
print(singers[17:21])
(chp08_slice1)
The slice operator [n:m]
returns the part of the string from the n’th character
to the m’th character, including the first but excluding the last. In other words, start with the character at index n and
go up to but do not include the character at index m.
This
behavior may seem counter-intuitive but if you recall the range
function, it did not include its end
point either.
If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the second index, the slice goes to the end of the string.
fruit = "banana"
print(fruit[:3])
print(fruit[3:])
(chp08_slice2)
What do you think fruit[:]
means?
Check your understanding
strings-7-1: What is printed by the following statements?
s = "python rocks"
print(s[3:8])
strings-7-2: What is printed by the following statements?
s = "python rocks"
print(s[7:11] * 3)
Note
This workspace is provided for your convenience. You can use this activecode window to try out anything you like.
(scratch_08_01)