Working with Strings: Indexing and Slicing
Slicing extracts a contiguous subset of a list using the syntax list[start:stop:step], where start is inclusive, stop is exclusive, and step is optional (default 1).
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…
From One Element to a Range
Indexing retrieves one element at a specific position. Slicing retrieves multiple elements by describing a range of positions. A slice creates a new list containing the selected elements, while the original list remains unchanged.
List slicing extracts a portion of a list with the notation list[start:stop:step]. The start position is included, the stop position is excluded, and the optional step controls the distance between selected positions.
Reading Slice Notation
Read list[start:stop:step] as a path through positions in a list. Start tells Python where selection begins. Stop tells Python where selection ends, but that position is not selected. Step tells Python how many positions to move between selected elements. All three parts are optional, and the default step is 1.
Tracing t[1:3]
Determine which elements are selected from the six-element list t containing a, b, c, d, e, and f.
Locate the start: The start index is 1, so selection begins with the element at index 1, which is b.
Stop before index 3: The stop index is 3, so the element at index 3, d, is not included.
List the visited positions: With the default step of 1, the selected positions are 1 and 2.
The slice contains b and c.
Tracing a Slice Boundary
What do you think happens?
A six-element list contains a, b, c, d, e, and f at indices 0 through 5. Which elements does the slice t[1:3] return?
Reveal answer
Answer: b and c
The start index 1 is included, while the stop index 3 is excluded. The slice therefore visits indices 1 and 2.
A reliable tracing method is to write the index positions first, mark the start position as included, and stop just before the stop position. For t[1:3], the path is index 1, then index 2. Index 3 is the boundary, not a selected element.
Omitted Boundaries
Leaving out a slice index does not leave the boundary undefined. When start is omitted, the slice begins at index 0. When stop is omitted, the slice continues to the end of the list. When both are omitted, the slice spans the entire list.
| Slice | Boundary meaning | Result for t containing a, b, c, d, e, f |
|---|---|---|
| t[:4] | Begin at index 0 and stop before index 4 | a, b, c, d |
| t[3:] | Begin at index 3 and continue to the end | d, e, f |
| t[:] | Begin at the start and continue to the end | a, b, c, d, e, f |
Omitted start and stop indices provide default slice boundaries.
Counting Backward
Negative indices count positions from the end of a list. The index -1 refers to the last element, -2 refers to the second-to-last element, and the count continues backward. Negative indices follow the same slice rule as positive indices: start is inclusive and stop is exclusive.
Selecting Around the Ends
Use a slice to exclude the first and last elements of a list.
Choose the first boundary: Start at index 1 so the first element is skipped.
Choose the negative stop: Use -1 as the stop boundary. It refers to the last element, but the stop position is exclusive.
Trace the selection: The slice t[1:-1] selects all elements after the first and before the last.
t[1:-1] extracts the elements between the first and last elements.
Changing the Step
The step controls how many positions are skipped between selected elements. A step of 1 selects every element. A step of 2 selects every other element, and a step of 3 selects every third element. A negative step can be used to reverse the order of elements.
Selecting Every Other Element
Determine the elements selected by t[::2] from a list containing a, b, c, d, e, and f.
Apply the omitted boundaries: The omitted start and stop mean that selection spans the whole list.
Apply the step: The step is 2, so the selected positions are 0, 2, and 4.
Map positions to elements: Positions 0, 2, and 4 contain a, c, and e.
The slice contains a, c, and e.
Common Slice Mistakes
Including the stop index
The stop boundary is exclusive.
Fix:
Trace the positions from 1 up to, but not including, 3. The selected positions are 1 and 2.Assuming an omitted start means index 1
An omitted start begins at index 0.
Fix:
Use index 0 as the default start.Forgetting that an omitted stop reaches the end
An omitted stop extends the slice to the end of the list.
Fix:
Include the element at index 3 and every remaining element.Treating a slice as the original list
Slicing creates a new list containing the selected elements.
Fix:
Treat the sliced list as an independent list.Ignoring the step
A step of 2 selects every other element.
Fix:
Trace positions 0, 2, 4, and so on.
A Reliable Tracing Routine
- Write the list elements with their positive indices.
- If negative indices appear, map them from the end, remembering that -1 is the last element.
- Fill in omitted boundaries: start defaults to the beginning, stop defaults to the end, and step defaults to 1.
- Mark the start position as included.
- Stop before the stop position because the stop boundary is exclusive.
- Move by the step between selected positions, reversing direction when the step is negative.
- Translate the selected positions back into their list elements.
A list contains a, b, c, d, e, and f at indices 0 through 5. Predict the selected elements for t[1:5:2], t[-4:-1], and t[::-1]. Explain the positions visited in each case.
Hints
- For t[1:5:2], begin at index 1, stop before index 5, and move two positions at a time.
- For t[-4:-1], map -4 and -1 to positions counted from the end, then apply the exclusive stop rule.
- For t[::-1], use the omitted boundaries and a negative step to reverse the order.
Key Takeaways
- The syntax list[start:stop:step] selects multiple elements from a list.
- Start is inclusive, stop is exclusive, and step controls the distance between selected positions.
- An omitted start begins at index 0, an omitted stop continues to the end, and omitting both creates a new list containing all elements.
- Negative indices count from the end, with -1 referring to the last element.
- Slicing does not modify the original list; it creates a new list containing the selected elements.
Key Takeaways
- A slice selects a range of list positions rather than one element.
- The start position is included, but the stop position is excluded.
- Omitted boundaries use the beginning or end of the list, and the default step is 1.
- Negative indices count backward from the end of the list.
- The step can skip positions or reverse the order, and every slice produces a new list.