Python Slicing ([start:stop:step] & slice()) Explained Visually
Master slicing in Python visually: test [start:stop:step] in a live playground, trace negative indices, reverse with [::-1], and use slice() objects.
Python slicing (sequence[start:stop:step]) extracts a subsequence from any string, list, tuple, or bytes object in a single expression—without writing a for loop.
Test any start, stop, and step combination live in the interactive playground below before reading the rules:
[start:stop:step]Why this happens: Starts at index 1 and stops BEFORE index 5 (length = 5 - 1 = 4 items).
1. Syntax: How sequence[start:stop:step] Works
Every Python slice takes up to three optional integers separated by colons:
sub = sequence[start : stop : step]
| Parameter | Default (when step > 0) | Default (when step < 0) | Meaning |
|---|---|---|---|
start | 0 (beginning) | len - 1 (last item) | Inclusive index where the slice begins |
stop | len(sequence) | -len - 1 (before index 0) | Exclusive index — Python stops before reaching stop! |
step | 1 (left → right) | (must be non-zero) | Stride between picked elements (-1 walks backward) |
The 1-Second Mental Model: Picture slice numbers as fenceposts between characters, not labels on boxes.
text[1:5]cuts at post1and post5, giving you exactly5 - 1 = 4characters (indices 1, 2, 3, 4).
2. The 6 Essential Python Slicing Patterns You Will Use Daily
nums = [10, 20, 30, 40, 50, 60]
# idx: 0 1 2 3 4 5
# neg: -6 -5 -4 -3 -2 -1
print(nums[1:4]) # [20, 30, 40] -> indices 1, 2, 3 (stops before 4!)
print(nums[:3]) # [10, 20, 30] -> first 3 elements
print(nums[3:]) # [40, 50, 60] -> from index 3 to the end
print(nums[-2:]) # [50, 60] -> last 2 elements (cleanest idiom!)
print(nums[:-1]) # [10, 20, 30, 40, 50] -> everything EXCEPT the last item
print(nums[::2]) # [10, 30, 50] -> every 2nd element (even indices)
print(nums[::-1]) # [60, 50, 40, 30, 20, 10] -> reversed copy in O(N)!
3. Reversing & Negative step Gotcha (nums[5:1:-1] vs nums[1:5:-1])
When step is negative (like -1 or -2), Python walks right-to-left. That flips one critical rule:
startmust sit to the RIGHT ofstop!- If you write
nums[1:5:-1], Python starts at index1and tries to walk left toward index5—which is already behind it—so it immediately returns[](empty list)!
word = "PYTHON"
# ❌ Common bug: start (1) is left of stop (4), but step (-1) walks left!
print(word[1:4:-1]) # "" (empty string!)
# ✅ Correct: start at index 4 ('O') and walk left, stopping BEFORE index 1 ('Y')
print(word[4:1:-1]) # "OHT" (indices 4, 3, 2)
4. What Is the Built-In slice() Function in Python?
Whenever you write items[1:5:2], CPython secretly translates it into items[slice(1, 5, 2)]. Calling slice(start, stop, step) directly lets you name fixed-width slices so teammates don't have to guess what magic numbers mean:
record = "2026-12-01 ORD-9941 INR 01499"
DATE_SLICE = slice(0, 10)
ORDER_SLICE = slice(12, 20)
AMOUNT_SLICE = slice(26, 31)
print(record[DATE_SLICE]) # "2026-12-01"
print(record[ORDER_SLICE]) # "ORD-9941"
print(int(record[AMOUNT_SLICE])) # 1499
5. List Slice Assignment & The Shallow-Copy Trap
Unlike immutable str and tuple objects, Python list objects let you assign to a slice to replace, insert, or delete chunks in-place:
cart = ["tea", "milk", "sugar", "bread"]
# Replace 2 items with 1 item in-place
cart[1:3] = ["coffee"]
print(cart) # ['tea', 'coffee', 'bread']
# Shallow copy warning on 2D nested lists:
grid = [[1, 2], [3, 4]]
clone = grid[:] # Copies outer list ONLY — inner lists are still shared!
clone[0][0] = 999
print(grid[0][0]) # 999! (Use copy.deepcopy(grid) for nested lists)
Quick Check — Python Slicing Mastery
Master the Complete Curriculum Visually
Liked this interactive breakdown? Jump into our full, 100% free A–Z visual course with zero sign-up wall.
Python Slicing ([start:stop:step] & slice()) Explained Visually — FAQ
Quick answers to the most common edge cases and interview questions.
Why does Python slicing exclude the stop index?
Excluding stop makes slice length math effortless: len(seq[start:stop]) is always stop - start, and two adjacent slices seq[:k] + seq[k:] split a sequence cleanly at k with zero overlap or missing items.
Does Python slicing raise an IndexError if stop is larger than the list length?
No. Unlike single-element indexing (nums[99] -> IndexError), Python slices clamp out-of-bounds start and stop values safely to the sequence boundaries, so nums[2:999] simply returns everything from index 2 to the end.
What is the difference between [start:stop:step] and Python's built-in slice() function?
They execute the exact same bytecode! Writing items[1:5:2] is syntactic sugar for items[slice(1, 5, 2)]. The slice() object lets you name reusable column ranges (like PRICE = slice(10, 18)) instead of scattering magic numbers.
Does nums[:] create a deep copy of a Python list?
No — nums[:] creates a new outer list (a shallow copy), which is safe for flat lists of numbers or strings, but nested inner lists still share memory references. Use copy.deepcopy(nums) for nested lists.