Get AI summaries of any video or article — Sign up free
Python Tutorial: Slicing Lists and Strings thumbnail

Python Tutorial: Slicing Lists and Strings

Corey Schafer·
4 min read

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.

TL;DR

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?

Because the end index is non-inclusive. For a list indexed 0–9, 0:5 returns elements at indices 0, 1, 2, 3, and 4—stopping before index 5. To include index 5, the slice must end at 6 (e.g., 0:6).

How do negative indexes change what you can extract?

Negative indexes count from the end of the sequence: -1 is the last element, -2 is the second-to-last, and so on. For example, list[-1] returns the last value (index 9 in the example list), while list[-10] returns the first value (index 0). Negative indexes can also be used as slice boundaries, and they can be mixed with positive boundaries.

What role does step play, and how does step=2 differ from step=1?

Step controls the stride between selected elements. With step=1 (the default), every element in the range is included. With step=2, the slice skips one element each time, so it returns every second element. In the example, slicing from index 2 to 8 with step=2 yields values at indices 2, 4, 6, and 8.

When does a negative step produce an empty result?

A negative step reverses traversal, but the start and end bounds must be compatible with that direction. If the slice tries to move “the wrong way” (for example, starting at a lower index while using a negative step to reach a higher end bound), Python can return an empty list because it never finds valid indices within the specified bounds.

How can the same slicing rules remove parts of a URL string?

Strings slice like lists. To reverse a URL, use : : -1 (empty start and end, step=-1). To remove the scheme and slashes (like “http://”), slice from a positive start index to the end (e.g., start at index 7). To remove both the scheme and the top-level domain, slice from index 7 up to a negative end index (end remains non-inclusive), which excludes the dot before the TLD.

Review Questions

  1. Given a list with indices 0–9, what slice would return elements at indices 3 through 7 inclusive?
  2. What slice expression reverses a list entirely, and why does it work?
  3. For a string, how would you slice to remove the first 8 characters while keeping everything else?

Key Points

  1. 1

    Use the slicing form start:end:step to extract ranges from lists and strings.

  2. 2

    Remember that the end index in Python slicing is non-inclusive, so end=5 excludes index 5.

  3. 3

    Negative indexes count from the end (-1 is last), and they can be used as slice boundaries.

  4. 4

    The step parameter controls skipping (step=2 takes every other element) and can be negative to reverse traversal.

  5. 5

    Leaving start or end blank defaults to the beginning or end of the sequence, respectively.

  6. 6

    A negative step requires start/end bounds that align with reverse direction, or the slice can be empty.

Highlights

End bounds are non-inclusive: 0:5 returns indices 0–4, while 0:6 returns 0–5.
Negative step enables reversing, but incompatible start/end directions can yield an empty slice.
Strings slice exactly like lists, making URL parsing via character ranges straightforward.
Using step=2 (or other values) turns slicing from “contiguous range” into “patterned extraction.”

Topics

  • List Slicing
  • String Slicing
  • Negative Indexing
  • Slice Step
  • Reversing Sequences

Mentioned