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.
- 1Locate the start
The start index is 1, so selection begins with the element at index 1, which is b.
- 2Stop before index 3
The stop index is 3, so the element at index 3, d, is not included.
- 3List 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
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.
- 1Choose the first boundary
Start at index 1 so the first element is skipped.
- 2Choose the negative stop
Use -1 as the stop boundary. It refers to the last element, but the stop position is exclusive.
- 3Trace 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.
- 1Apply the omitted boundaries
The omitted start and stop mean that selection spans the whole list.
- 2Apply the step
The step is 2, so the selected positions are 0, 2, and 4.
- 3Map positions to elements
Positions 0, 2, and 4 contain a, c, and e.
The slice contains a, c, and e.
Common Slice Mistakes
Common mistake Including the stop index
What it looks likeTreating t[1:3] as if it included the element at index 3
Why it failsThe stop boundary is exclusive.
Do this insteadTrace the positions from 1 up to, but not including, 3. The selected positions are 1 and 2.
Common mistake Assuming an omitted start means index 1
What it looks likeReading t[:4] as if selection begins with the second element
Why it failsAn omitted start begins at index 0.
Do this insteadUse index 0 as the default start.
Common mistake Forgetting that an omitted stop reaches the end
What it looks likeReading t[3:] as if it stops immediately after index 3
Why it failsAn omitted stop extends the slice to the end of the list.
Do this insteadInclude the element at index 3 and every remaining element.
Common mistake Treating a slice as the original list
What it looks likeExpecting a modification to a sliced list to modify the original list
Why it failsSlicing creates a new list containing the selected elements.
Do this insteadTreat the sliced list as an independent list.
Common mistake Ignoring the step
What it looks likeExpecting t[::2] to select every element
Why it failsA step of 2 selects every other element.
Do this insteadTrace positions 0, 2, 4, and so on.
A Reliable Tracing Routine
- 1Write the list elements with their positive indices.
- 2If negative indices appear, map them from the end, remembering that -1 is the last element.
- 3Fill in omitted boundaries: start defaults to the beginning, stop defaults to the end, and step defaults to 1.
- 4Mark the start position as included.
- 5Stop before the stop position because the stop boundary is exclusive.
- 6Move by the step between selected positions, reversing direction when the step is negative.
- 7Translate 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
- 1The syntax list[start:stop:step] selects multiple elements from a list.
- 2Start is inclusive, stop is exclusive, and step controls the distance between selected positions.
- 3An omitted start begins at index 0, an omitted stop continues to the end, and omitting both creates a new list containing all elements.
- 4Negative indices count from the end, with -1 referring to the last element.
- 5Slicing does not modify the original list; it creates a new list containing the selected elements.
At a glance
- 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.