Section 7.6 The Slice Operator
One of the string manipulations we mentioned in
Section 7.1 was extracting a part of a string. For example, separating
408-555-1212
into area code, prefix, and number; or separating
New York, NY 10280
into city, state, and ZIP (postal) code.
Another name for a part of a string is a substring. In Python, a substring of a string is called a slice. Selecting a slice is similar to selecting a character.
The slice operator [n:m]
returns the part of the string starting at character number n
up to, but not including, character number m
. This behavior may seem counter-intuitive, but if you recall the range
function, it does not include its end point either. The following example shows slices in action:
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.
There is no Index Out Of Range exception for a slice. A slice is forgiving and shifts any offending index to something legal. In terms of programming style, we do not put a space on both sides of the :
in a slice.
What do you think veggie[:]
means? Put it into the activecode and find out!
Checkpoint 7.6.1.
What is printed by the following statements?
s = "python rocks"
print(s[3:8])
python
- That would be
s[0:6]
.
rocks
- That would be
s[7:]
.
hon r
- Yes, start with the character at index 3 and go up to but not including the character at index 8.
Error, you cannot have two numbers inside the [ ].
- This is called slicing, not indexing. It requires a start and an end.
Checkpoint 7.6.2.
What is printed by the following statements?
s = "python rocks"
print(s[7:11] * 3)
rockrockrock
- Yes, rock starts at 7 and goes through 10. Repeat it 3 times.
rock rock rock
- Repetition does not add a space.
rocksrocksrocks
- Slicing will not include the character at index 11. Just up to it (10 in this case).
Error, you cannot use repetition with slicing.
- The slice will happen first, then the repetition. So it is ok.
Checkpoint 7.6.3.
Write a program to separate a string like "New York, NY 10280"
into city, state, and ZIP (postal) code. The program should work for any similarly formatted string, such as "Ashtabula, OH 44005"
or "Sault Ste. Marie, MI 49783"
. This program requires some planning. If we look at these addresses, we note these three things:
Everything before the comma is the city name.
The comma has a space after it. The two letters after that space are the state abbreviation.
The last five characters in the string are the ZIP code.
If we look at everything after the city name (the comma, space, two-character state, space, and five digits), we find that adds up to ten characters. That means the first length - 10 characters must be the city name, where length is the length of the whole string.
Use these facts to figure out the slices required to separate out the city, state, and ZIP code.
Solution.address = "Sault Ste. Marie, MI 49783"
city_end = len(address) - 10
city = address[0:city_end]
state = address[(city_end + 2):(city_end + 4)]
zipcode = address[-5:]
# display vertical bars to make sure there
# are no extra spaces.
print(f"City: |{city}|")
print(f"State: |{state}|")
print(f"ZIP code: |{zipcode}|")