Python Slicing — [start:stop:step] Visualized
"hello"[::2] # → "hlo"What does each position mean? Let’s break it down.
The Three Slots
sequence[ start : stop : step ]| Slot | Default | Meaning |
|---|---|---|
start |
0 (or -1 for negative step) |
Where to begin |
stop |
len(seq) (or -len(seq)-1 for negative step) |
Where to stop (exclusive) |
step |
1 |
How many to skip each time |
Each slot is optional. Leave it blank and Python fills in the default.
:: — Only Step, No Bounds
"hello"[::2] # start=0, stop=len, step=2 → "hlo"
"hello"[::1] # start=0, stop=len, step=1 → "hello"
"hello"[::-1] # start=-1, stop=-len-1, step=-1 → "olleh"Visual walkthrough of "hello"[::2]:
Index: 0 1 2 3 4
h e l l o
──────────────────
Step 1: h ← index 0, take it
e ← index 1, skip
l ← index 2, take it
l ← index 3, skip
o ← index 4, take it
Result: h l o"hello"[::-1] (reverse):
Index: 0 1 2 3 4
h e l l o
──────────────────
Step -1: o ← index 4, take it
l ← index 3, take it
l ← index 2, take it
e ← index 1, take it
h ← index 0, take it
Result: o l l e h1: — Start From Index 1
"hello"[1:] # start=1, stop=len, step=1 → "ello" h e l l o
0 1 2 3 4
──────────────
e l l o:3 — Stop Before Index 3
"hello"[:3] # start=0, stop=3, step=1 → "hel" h e l l o
0 1 2 3 4
──────────
h e l1:4 — From 1 to 4 (exclusive)
"hello"[1:4] # start=1, stop=4, step=1 → "ell" h e l l o
0 1 2 3 4
──────────
e l l1:4:2 — From 1 to 4, Every 2nd
"hello"[1:4:2] # → "el"Index: 0 1 2 3 4
h e l l o
──────────
Step 2: e ← index 1, take it
l ← index 2, skip
l ← index 3, take it
← index 4, stop (exclusive)
Result: e lNegative Indices — Counting From the End
Index: -5 -4 -3 -2 -1
h e l l o
0 1 2 3 4"hello"[-3:] # start=-3, stop=len → "llo"
"hello"[:-2] # start=0, stop=-2 → "hel"
"hello"[-4:-1] # start=-4, stop=-1 → "ell"
"hello"[-2:] # "lo"
"hello"[:-1] # "hell"Negative Step — Reverse Direction
When step is negative, start defaults to -1 and stop defaults to -len-1.
"hello"[::-1] # "olleh"
"hello"[3::-1] # start=3, keep going left → "lleh"
"hello"[:2:-1] # stop=2 (exclusive), start=-1 → "ol"Walkthrough of "hello"[3::-1]:
Index: 0 1 2 3 4
h e l l o
← start=3, step=-1
l ← index 3, take it
l ← index 2, take it
e ← index 1, take it
h ← index 0, take it
← past -1, stop
Result: l l e hLive Animation Table
Print this and watch the index move:
"hello"[1:4:2]
Index 0 1 2 3 4
h e l l o
─────────────────────────────────
Cursor │
Take/Skip? skip take skip take stop
↑stop(4)
↑take= l
Result e lStep-by-Step Simulator (mental model)
# This is what Python does internally:
def slice_visualize(seq, start, stop, step):
i = start
result = []
if step > 0:
while i < stop:
result.append((i, seq[i], "✓" if (i - start) % step == 0 else "—"))
i += 1
else:
while i > stop:
result.append((i, seq[i], "✓"))
i += step
return result
# For "hello"[::2]:
# (0, 'h', '✓')
# (1, 'e', '—')
# (2, 'l', '✓')
# (3, 'l', '—')
# (4, 'o', '✓')Reference: All in One
s = "abcdef"
s[:] # "abcdef" (full copy)
s[::] # "abcdef" (full copy)
s[::1] # "abcdef" (default step)
s[::-1] # "fedcba" (reverse)
s[2:] # "cdef" (from 2 onward)
s[:3] # "abc" (up to 3)
s[1:4] # "bcd" (range)
s[::2] # "ace" (every other)
s[1::2] # "bdf" (every other from 1)
s[::3] # "ad" (every third)
s[-1] # "f" (last item)
s[:-1] # "abcde" (all but last)
s[-3:] # "def" (last 3)
s[-3::-1] # "dcba" (from -3 backward)
s[3:0:-1] # "dcb" (from 3 to 1, backward)
# Works on any sequence — strings, lists, tuples
list(range(10))[::2] # [0, 2, 4, 6, 8]
(1, 2, 3, 4, 5)[::-1] # (5, 4, 3, 2, 1)Common Patterns
# Reverse
text[::-1]
# Palindrome check
s == s[::-1]
# Every other element
items[::2]
# Remove first and last
items[1:-1]
# First, third, fifth...
items[::2]
# Second, fourth, sixth...
items[1::2]
# Last 5
items[-5:]
# All but last 3
items[:-3]
# Rotate left by 1
items[1:] + items[:1]
# Rotate right by 1
items[-1:] + items[:-1]Visual Cheat Sheet
Positive step (→):
[start:stop:step]
↓ ↓ ↓
where to where to how many
begin stop to skip
(exclusive)
Negative step (←):
[start:stop:step]
↓ ↓ ↓
where to where to go backwards
begin stop
(exclusive)
Indices:
────┬───┬───┬───┬───┬───
0 1 2 3 4 5 (positive)
-5 -4 -3 -2 -1 (negative)Golden rule: [start:stop:step] — think of the colons as delimiters for three separate instructions. Leave any blank for “use the default.”