Python Tutorial: Slicing Lists and Strings
Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Use the slicing form start:end:step to extract ranges from lists and strings.
Briefing
Python slicing lets you extract parts of lists and strings using a compact syntax: start, end, and optional step (start:end:step). The core rule is that the end index is non-inclusive, meaning slicing stops right before the end value. That single detail drives most “off-by-one” outcomes when pulling ranges like 0 through 5 (which actually returns indices 0–4 unless the end is set to 6).
For lists, indexing can be positive or negative. Positive indexes count from the front (0 is the first element), while negative indexes count from the back (-1 is the last element). Using slicing, you can pull contiguous ranges by specifying start and end separated by a colon—such as 0:5 to get elements at indices 0–4, or 0:6 to include the element at index 5. The same mechanism works for any range, including ones that start and end with different sign conventions: for example, mixing positive and negative indexes can produce the same subset as using only positive indexes.
When the goal is not just a continuous range but a pattern, the step parameter controls how many elements to skip. With step=2, slicing returns every second element within the chosen bounds (e.g., 2:9:2 yields values at indices 2, 4, 6, and 8). The default step is 1, which effectively means “take every element.” Step can also be negative to traverse in reverse. A negative step only works when the start and end directions align; otherwise the slice can come back empty. Reversing a list is a special case of this rule: leaving start and end blank and using step=-1 returns the entire list in reverse order.
Strings follow the exact same slicing logic as lists because strings are sequences of characters. Reversing a string uses the same pattern—empty start and end with step=-1. Extracting the top-level domain from a URL can be done by slicing from a negative index near the end; for instance, starting at -4 and letting the default step run forward captures the “.com”-style suffix. Removing the “http://” prefix is handled by slicing from a positive start index to the end of the string (e.g., starting at index 7). A combined slice can remove both the scheme and the top-level domain by selecting a start index and an end index with the end remaining non-inclusive, ensuring the dot before the TLD is excluded.
Overall, the tutorial’s practical takeaway is that slicing is less about memorizing many separate operations and more about mastering three consistent mechanics: negative indexing, non-inclusive end bounds, and step-based skipping or reversing. Once those are clear, the same syntax reliably extracts the exact segments needed from both lists and strings.
Cornell Notes
Slicing in Python uses the syntax start:end:step to extract portions of lists and strings. Indexing supports both positive (from the front) and negative (from the back) values, with -1 meaning the last element. The end index is non-inclusive, so slice end values are excluded from the result. The step parameter controls skipping (step=2 takes every other element) and can be negative to traverse in reverse. Leaving start or end blank uses the sequence boundary, and leaving both blank with step=-1 reverses the entire sequence.
Why does slicing like 0:5 often surprise beginners?
How do negative indexes change what you can extract?
What role does step play, and how does step=2 differ from step=1?
When does a negative step produce an empty result?
How can the same slicing rules remove parts of a URL string?
Review Questions
- Given a list with indices 0–9, what slice would return elements at indices 3 through 7 inclusive?
- What slice expression reverses a list entirely, and why does it work?
- For a string, how would you slice to remove the first 8 characters while keeping everything else?
Key Points
- 1
Use the slicing form start:end:step to extract ranges from lists and strings.
- 2
Remember that the end index in Python slicing is non-inclusive, so end=5 excludes index 5.
- 3
Negative indexes count from the end (-1 is last), and they can be used as slice boundaries.
- 4
The step parameter controls skipping (step=2 takes every other element) and can be negative to reverse traversal.
- 5
Leaving start or end blank defaults to the beginning or end of the sequence, respectively.
- 6
A negative step requires start/end bounds that align with reverse direction, or the slice can be empty.