How Does Slicing Work in Python?
Understand Python slicing with start:stop:step syntax, negative indices, steps, reversing sequences and safe out-of-range behavior, with clear examples.
Expected Interview Answer
Slicing extracts a portion of a sequence using the syntax sequence[start:stop:step], returning a new sequence that includes start up to but not including stop, moving by step.
It works on lists, strings, tuples and other sequences. All three parts are optional: start defaults to the beginning, stop to the end, and step to 1. Negative indices count from the end, and a negative step reverses direction (so [::-1] reverses the sequence). Slicing never raises an out-of-range error — it silently clamps to valid bounds — and it always returns a new object rather than mutating the original.
- Concise extraction of sub-sequences without loops
- Never raises IndexError — bounds are clamped safely
- Supports negative indices and steps for flexible access
- [::-1] reverses any sequence in one expression
- Returns a shallow copy, leaving the original untouched
AI Mentor Explanation
Slicing is like pulling only overs 10 through 20 from a full match recording to study the middle phase. You state where to start and where to stop, and you get exactly that window — the ball at the stop over is excluded, just as slice[start:stop] leaves out the stop index while keeping everything before it.
Step-by-Step Explanation
Step 1
Write the brackets
Use sequence[start:stop:step]; any of the three may be omitted and defaults will fill in.
Step 2
Apply the half-open rule
The result includes the start index and every index up to but not including stop.
Step 3
Use defaults
Leave start blank for the beginning, stop blank for the end, step blank for 1 — e.g. list[:] copies everything.
Step 4
Try negative values
Negative indices count from the end; list[-3:] takes the last three items.
Step 5
Step and reverse
A step of 2 skips every other item; a step of -1 walks backward, so list[::-1] reverses the sequence.
What Interviewer Expects
- The start:stop:step syntax and its defaults
- Understanding that stop is exclusive (half-open range)
- Knowledge of negative indices and negative steps
- Awareness that slicing returns a new (shallow) copy
- That out-of-range slices are clamped, not errors
Common Mistakes
- Assuming the stop index is included in the result
- Thinking an out-of-range slice raises IndexError
- Believing slicing mutates the original sequence
- Confusing negative-index slicing direction with a negative step
- Forgetting that a bare [:] makes a shallow copy, not a deep one
Best Answer (HR Friendly)
“Slicing is a quick way to grab a chunk of a list or string in Python by saying where to start and where to stop. It hands back a new copy of just that section without changing the original, and it can even count from the end or run backwards.”
Code Example
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(nums[2:5]) # [2, 3, 4] -> stop index 5 excluded
print(nums[:3]) # [0, 1, 2] -> start defaults to 0
print(nums[7:]) # [7, 8, 9] -> stop defaults to end
print(nums[::2]) # [0, 2, 4, 6, 8] -> every other item
print(nums[-3:]) # [7, 8, 9] -> last three
print(nums[::-1]) # [9, 8, ... 0] -> reversed
text = 'python'
print(text[1:4]) # 'yth'
print(text[::-1]) # 'nohtyp'
# Slicing never raises for out-of-range bounds
print(nums[5:100]) # [5, 6, 7, 8, 9] -> clamped safelyFollow-up Questions
- How do you reverse a list or string using slicing?
- What does the step parameter do in a slice?
- Is a slice a shallow copy or a deep copy?
- How do negative indices behave in slicing?
- Why does an out-of-range slice not raise an error?
MCQ Practice
1. What does nums[1:4] return for nums = [0,1,2,3,4]?
Slicing is half-open: it includes index 1, 2 and 3 but excludes the stop index 4.
2. Which expression reverses a sequence?
A step of -1 with empty start and stop walks the sequence backward, producing a reversed copy.
3. What does nums[3:100] return for a 5-element list nums?
Slicing clamps out-of-range bounds, so it safely returns items from index 3 through the last element.
Flash Cards
What is the slice syntax? — sequence[start:stop:step] — start included, stop excluded, moving by step; all parts optional.
How to reverse with slicing? — Use seq[::-1], a step of -1 with default start and stop.
Does slicing copy or mutate? — It returns a new shallow copy; the original sequence is unchanged.
What if bounds are out of range? — Slicing clamps to valid limits and never raises IndexError.