String Methods for Searching and Replacing
A slice extracts a substring using [n:m], where n is the starting index (inclusive) and m is the stopping index (exclusive).
A String as Indexed Characters
A string can be treated as a sequence of characters with index positions. String slicing selects part of that sequence and produces a substring. The notation [n:m] identifies a starting index n and a stopping index m.
The key prediction habit is to read a slice in two parts: include the character at the start index, then continue toward the stop index, but stop just before the character at that stop index.
Reading the Index Range
In [n:m], n is the starting index and is included in the result. The stopping index m is excluded from the result.
Selecting the Middle Characters
For the string "python", determine which characters are selected by the slice [1:4].
Locate the start: Index 1 contains the character y, and the start index is included.
Move toward the stop: Continue through indices 2 and 3, which contain t and h.
Exclude the stop: Index 4 contains o, but the stop index is excluded.
The selected substring is "ytho".
Why the Stop Is Excluded
The exclusive stop makes the number of selected characters easy to calculate. A slice from index i to index i+n selects n consecutive characters, written as [i:i+n]. The endpoint marks where selection ends rather than adding another character to the result.
Omitting an Endpoint
Either endpoint may be omitted. Omitting the start index means the slice begins at the beginning of the string. Omitting the stop index means the slice continues to the end of the string.
| Notation | Meaning | Generated example |
|---|---|---|
| [:m] | Start at the beginning and stop before index m | "python"[:3] selects "pyt" |
| [n:] | Start at index n and continue to the end | "python"[3:] selects "hon" |
Omitted start and stop indices
From Full String to Substring
Predicting Several Slices
For the string "planet", determine the selected substring for [1:5], [:3], and [4:].
[1:5]: Start at index 1 and include indices 1, 2, 3, and 4. The character at index 5 is the stop boundary.
[:3]: The omitted start means begin at the beginning, then stop before index 3.
[4:]: Start at index 4 and continue through the end because the stop index is omitted.
[1:5] selects "lane", [:3] selects "pla", and [4:] selects "et".
What do you think happens?
For the string "python", which characters are selected by [0:5]?
Reveal answer
Answer: "pytho"
The slice starts at index 0 and stops before index 5, so it includes indices 0, 1, 2, 3, and 4. The character at index 5 is excluded.
Mistakes with Slice Boundaries
Including the stop index in the result
The stop index is exclusive, so [0:5] includes indices 0 through 4 only.
Fix:
Treat the stop index as the ending boundary and stop immediately before it.Counting characters instead of reading index positions
The notation refers to index positions, and the selected characters depend on both the start and stop positions.
Fix:
Write or inspect the index for each character, include the start, and exclude the stop.Assuming an omitted endpoint means an empty selection
An omitted start means the slice begins at the beginning of the string.
Fix:
Interpret [:m] as beginning-to-m and [n:] as n-to-the-end.
When predicting a slice, make a short index map first. Mark the start index as included, mark the stop index as excluded, and then read the characters between those boundaries.
Slice Prediction Practice
For the string "computer", determine the selected characters for [0:4], [3:7], [:2], and [5:]. For each answer, identify the first included index and the excluded stop boundary, if one is present.
Hints
- The start index is included.
- The stop index is excluded.
- An omitted start begins at the beginning, and an omitted stop continues to the end.
Checking the Practice Pattern
For the string "computer", determine the selected characters for [3:7].
Find the boundaries: The selection begins at index 3 and ends before index 7.
List the included positions: The included indices are 3, 4, 5, and 6.
Read the characters: Those positions contain p, u, t, and e.
The selected substring is "pute".
What to Remember
- A slice uses [n:m] to extract a substring.
- The start index n is included, while the stop index m is excluded.
- The notation [:m] slices from the beginning to just before m.
- The notation [n:] slices from n through the end.
- The exclusive stop makes [i:i+n] select n consecutive characters.
Key Takeaways
- String slices extract substrings using a start index and a stop index.
- The start index is included, but the stop index is excluded.
- Omitting the start selects from the beginning; omitting the stop selects through the end.
- To predict a result, list the included index positions before reading their characters.
- The exclusive stop allows a slice such as [i:i+n] to select n consecutive characters.