Python Slicing: Master Indexing for Strings, Lists, and Tuples
Imagine a pizza cut into 8 slices, numbered 0 through 7. You don't always want the whole pizza. Sometimes you want slices 2 through 5. Sometimes you want every other slice. Sometimes you want the last three slices.
Python slicing lets you grab a portion of any sequence — a list, string, or tuple — using the same simple notation. It's one of the most elegant features in Python, and once you learn it, you'll use it everywhere.
In this tutorial, you'll learn the start:stop:step notation, how negative indices work, how to reverse any sequence, and the most common slicing patterns used by Python developers every day.
How Does Basic Slicing Work?
The basic slice syntax is sequence[start:stop]. It gives you items from the start index up to (but not including) the stop index. Think of it as a half-open interval.
The "stop before" rule might feel odd at first. But it has a nice benefit: fruits[0:3] gives you exactly 3 items. The number of items in a slice is always stop - start.
What Does the Step Parameter Do?
The full slice syntax is sequence[start:stop:step]. The step controls how many items to skip between each one you take. A step of 2 takes every other item. A step of 3 takes every third item.
When you use a step, the default start is 0 and the default stop is the end of the sequence. You only need to specify them if you want to narrow the range.
How Do Negative Indices Work in Slicing?
Negative indices count from the end of the sequence. -1 is the last item, -2 is the second-to-last, and so on. You can use negative numbers in any part of the slice: start, stop, or step.
Negative indices are incredibly useful when you know you want items relative to the end. For example, my_list[-3:] always gives you the last three items, no matter how long the list is.
How to Reverse a Sequence with [::-1]
A negative step tells Python to go backwards. The most common pattern is [::-1], which reverses the entire sequence. No start, no stop, step of -1 — just go from end to start.
You can combine a negative step with start and stop to reverse just a portion. When using a negative step, the start should be greater than stop since you're going backwards.
Slicing Strings vs Lists vs Tuples
The same [start:stop:step] syntax works on all sequence types. The only difference is what you get back: slicing a list gives you a list, slicing a string gives you a string, and slicing a tuple gives you a tuple.
Common Slicing Patterns Every Python Developer Should Know
Here are the slicing patterns you'll see and use most often. Think of this as your quick reference.
You can also use slicing on the left side of an assignment to replace a section of a list. This is called slice assignment.
Practice Exercises
Given the list animals, use slicing to print only the middle three items: ['cat', 'dog', 'elephant'].
What does this code print? Think carefully about negative indices.
word = 'programming'
print(word[-4:])Write the code and run it to check your prediction.
Given the list letters, use slicing with a step to print only the items at even indices (0, 2, 4, 6). The result should be ['a', 'c', 'e', 'g'].
Use slicing to reverse the string word and print the result. The output should be nohtyp.
This code tries to get the last three items from a list, but it's using the wrong slice. Fix the bug so it prints ['date', 'elderberry', 'fig'].
Write a function called is_palindrome(text) that returns True if the text reads the same forwards and backwards, and False otherwise. Use slicing to reverse the string. Convert the text to lowercase first so the check is case-insensitive.
Test it with these calls:
print(is_palindrome('racecar'))
print(is_palindrome('Python'))
print(is_palindrome('Madam'))