slicing
In Python, slicing is an operation that allows you to extract portions of sequences like lists, tuples, and strings by specifying a start
, stop
, and step
indices with the syntax sequence[start:stop:step]
.
In this syntax, start
is the index where the slice begins, stop
is the index where the slice ends (exclusive), and step
determines the stride between each element in the slice. If you omit start
, stop
, or step
, then they default to the beginning of the sequence, the end of the sequence, and a step of one, respectively.
With slicing, you can quickly obtain a subset of items without modifying the original sequence, making it an essential feature for any Python programmer.
Example
Here’s a quick example of how you can use slicing with a list:
>>> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> # Get a slice of numbers from index 2 to 5
>>> numbers[2:6]
[2, 3, 4, 5]
>>> # Get every other element from the list
>>> numbers[::2]
[0, 2, 4, 6, 8]
Related Resources
Tutorial
Python's list Data Type: A Deep Dive With Examples
In this tutorial, you'll dive deep into Python's lists. You'll learn how to create them, update their content, populate and grow them, and more. Along the way, you'll code practical examples that will help you strengthen your skills with this fundamental data type in Python.
For additional information on related topics, take a look at the following resources:
- Python's tuple Data Type: A Deep Dive With Examples (Tutorial)
- Lists vs Tuples in Python (Tutorial)
- Strings and Character Data in Python (Tutorial)
- Exploring Python's list Data Type With Examples (Course)
- Exploring Python's tuple Data Type With Examples (Course)
- Lists and Tuples in Python (Course)
- Lists vs Tuples in Python (Quiz)
- Strings and Character Data in Python (Course)
- Python Strings and Character Data (Quiz)
By Leodanis Pozo Ramos • Updated June 3, 2025