String Slicing
List slicing uses the syntax t[start:stop] to extract a portion of a list, where start is included and stop is excluded.
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…
The Boundary Rule
List slicing extracts a portion of a list with the syntax t[start:stop]. The start index is included, but the stop index is excluded. This means that t[1:3] selects the elements at indices 1 and 2, not the element at index 3. Learning to identify those two boundaries is the central skill in slicing.
What do you think happens?
Given values = ['a', 'b', 'c', 'd'], what does values[1:3] return?
Reveal answer
Answer: ['b', 'c']
Index 1 is included and index 3 is excluded, so the selected indices are 1 and 2.
Mapping Boundaries to Elements
Read a slice as a range of index positions. For t[1:3], begin at index 1, include that element, and continue up to but not including index 3. The resulting list therefore contains two elements: the values stored at indices 1 and 2. The stop number describes where selection ends; it is not another included position.
Selecting a Middle Portion
Determine the result of letters[2:5] when letters = ['a', 'b', 'c', 'd', 'e', 'f'].
Read the start: The start index is 2, so the element at index 2, 'c', is included.
Read the stop: The stop index is 5, so the element at index 5, 'f', is excluded.
List the selected indices: The included indices are 2, 3, and 4.
Translate indices to values: Indices 2, 3, and 4 contain 'c', 'd', and 'e'.
letters[2:5] produces ['c', 'd', 'e'].
Shortcuts with Omitted Indices
A slice does not require both boundary numbers. In t[:n], the omitted start means selection begins at the beginning of the list, and the result contains the first n elements. In t[n:], the omitted stop means selection continues from index n through the end of the list. In t[:], both boundaries are omitted, so the result is a copy of the entire list.
first_two is ['a', 'b']
from_index_two is ['c', 'd']
copy_of_values is ['a', 'b', 'c', 'd']Replacing a Slice
Slice assignment uses the form t[start:stop] = new_list. Instead of producing a separate sublist, this operation replaces the selected portion of the original list. The replacement list can have a different length from the selected slice. A longer replacement makes the original list grow, while a shorter replacement makes it shrink. Elements outside the selected slice remain unchanged.
letters = ['a', 'b', 'c', 'd', 'e', 'f'] letters[1:3] = ['x', 'y', 'z'] letters
Steps and Extended Slices
The full slice form is t[start:stop:step]. When the step is omitted, it defaults to 1, so every element between the boundaries is selected. A step of 2 selects every other element, and a step of 3 selects every third element. A negative step reverses the selection.
every_other is ['a', 'c', 'e']
every_third is ['a', 'd']
reversed_values is ['f', 'e', 'd', 'c', 'b', 'a']Mistakes Beginners Make
Including the stop index in the result
The stop index is excluded from a slice.
Fix:
Select the indices from the start through the position immediately before the stop.Treating t[:n] as a slice ending with index n
The omitted start begins at index 0, but the stop index 2 is still excluded.
Fix:
Remember that values[:2] returns the first two elements.Expecting slice assignment to preserve list length
The selected slice has two elements, while the replacement has three, so the list grows.
Fix:
Compare the lengths of the selected slice and replacement list to predict whether the original list grows or shrinks.Confusing extraction with modification
The first expression extracts a portion; the second replaces a portion of the original list.
Fix:
Check which side of the assignment operator contains the slice.
Prediction Practice
Before checking the results, predict each expression. Use the start-included and stop-excluded rule, then verify your predictions by running the code.
Hints
- For values[1:4], list indices 1, 2, and 3.
- For values[:3], begin at index 0 and stop before index 3.
- For values[3:], continue from index 3 through the end.
- For slice assignment, compare the number of selected elements with the number of replacement elements.
first_result is ['b', 'c', 'd']
second_result is ['a', 'b', 'c']
third_result is ['d', 'e']
final_result is ['a', 'x', 'd', 'e']Checking a Shrinking Assignment
Predict the result after values[1:3] = ['x'] when values = ['a', 'b', 'c', 'd', 'e'].
Identify the selected slice: Indices 1 and 2 are selected, so the original elements are 'b' and 'c'.
Compare lengths: Two elements are selected, but the replacement list contains one element.
Apply the replacement: The two selected elements are replaced by the single element 'x'.
Keep the other elements: The elements 'a', 'd', and 'e' remain, with 'd' and 'e' following the replacement.
The modified list is ['a', 'x', 'd', 'e']; it has shrunk by one element.
Key Takeaways
- Use t[start:stop] to select a portion of a list.
- The start index is included and the stop index is excluded.
- Use t[:n] for the first n elements, t[n:] for index n onward, and t[:] for a copy of the entire list.
- Use t[start:stop] = new_list to replace multiple elements in the original list.
- A replacement list can make the original list grow or shrink, and t[start:stop:step] can select elements at intervals.
Key Takeaways
- A slice includes its start index but excludes its stop index.
- Omitting the start selects from the beginning, omitting the stop selects through the end, and omitting both creates a copy of the entire list.
- Slice assignment changes the original list and can replace a selected slice with a shorter or longer list.
- The optional step controls the interval between selected elements, and a negative step reverses the selection.
- Predict the selected indices before running code, then compare the prediction with Python's result.