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).
Try it: Indexing and Slicing
How Python indexes a sequence from 0 (or from the end with negative numbers) and how a slice start:stop:step picks positions — with the exact CPython rules for omitted and out-of-range bounds.
How it works
- Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
- An index outside the string raises IndexError.
- A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
- A negative step walks backwards; a step of 0 is a ValueError.
Default run (7 steps): 'PYTHONIC'[1:6:2]: a slice copies characters from start up to (not including) stop, moving by step. … 'PYTHONIC'[1:6:2] → 'YHN' (positions 1, 3, 5).
Simplified: Works on a short string (up to 16 printable ASCII characters); lists slice by exactly the same rules.
Loading the simulation…
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].
- 1Locate the start
Index 1 contains the character y, and the start index is included.
- 2Move toward the stop
Continue through indices 2 and 3, which contain t and h.
- 3Exclude 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[1:5]
Start at index 1 and include indices 1, 2, 3, and 4. The character at index 5 is the stop boundary.
- 2[:3]
The omitted start means begin at the beginning, then stop before index 3.
- 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
"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
Common mistake Including the stop index in the result
What it looks likeExpecting [0:5] to include the character at index 5
Why it failsThe stop index is exclusive, so [0:5] includes indices 0 through 4 only.
Do this insteadTreat the stop index as the ending boundary and stop immediately before it.
Common mistake Counting characters instead of reading index positions
What it looks likeChoosing a slice without checking which index contains the first character
Why it failsThe notation refers to index positions, and the selected characters depend on both the start and stop positions.
Do this insteadWrite or inspect the index for each character, include the start, and exclude the stop.
Common mistake Assuming an omitted endpoint means an empty selection
What it looks likeReading [:m] as if no starting point were provided
Why it failsAn omitted start means the slice begins at the beginning of the string.
Do this insteadInterpret [: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].
- 1Find the boundaries
The selection begins at index 3 and ends before index 7.
- 2List the included positions
The included indices are 3, 4, 5, and 6.
- 3Read the characters
Those positions contain p, u, t, and e.
The selected substring is "pute".
What to Remember
- 1A slice uses [n:m] to extract a substring.
- 2The start index n is included, while the stop index m is excluded.
- 3The notation [:m] slices from the beginning to just before m.
- 4The notation [n:] slices from n through the end.
- 5The exclusive stop makes [i:i+n] select n consecutive characters.
At a glance
- 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.