Interactive Visual Guide6 min read

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:

Interactive Python Slicing Playground — [start:stop:step]
Click any preset or edit start, stop, and step to see positive & negative indices live
Try:
+0'P'-8skip
+1'Y'-7#1
+2'T'-6#2
+3'H'-5#3
+4'O'-4#4
+5'N'-3skip
+6'I'-2skip
+7'C'-1skip
Top row = Positive index (0 to 7)Bottom row = Negative index (-8 to -1)
text = "PYTHONIC"
>>> text[1:5] # same as text[slice(1, 5, None)]
"YTHO" (len = 4)

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:

PYTHON
sub = sequence[start : stop : step]
ParameterDefault (when step > 0)Default (when step < 0)Meaning
start0 (beginning)len - 1 (last item)Inclusive index where the slice begins
stoplen(sequence)-len - 1 (before index 0)Exclusive index — Python stops before reaching stop!
step1 (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 post 1 and post 5, giving you exactly 5 - 1 = 4 characters (indices 1, 2, 3, 4).

Visual: Positive & Negative String Indexes for `s = "PYTHON"`
Positive →← Negative0P-61Y-52T-43H-34O-25N-1s[1:4] → "YTH" (starts at 1, stops BEFORE 4!)s[-1] → "N" | s[::2] → "PTO" | s[::-1] → "NOHTYP" (instant reverse!)

2. The 6 Essential Python Slicing Patterns You Will Use Daily

PYTHON
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:

  • start must sit to the RIGHT of stop!
  • If you write nums[1:5:-1], Python starts at index 1 and tries to walk left toward index 5—which is already behind it—so it immediately returns [] (empty list)!
PYTHON
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:

PYTHON
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:

PYTHON
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

Q1.What does 'ABCDEF'[1:5:2] evaluate to?
Q2.What does [10, 20, 30, 40][1:100] return in Python?
Continue Learning for Free

Master the Complete Curriculum Visually

Liked this interactive breakdown? Jump into our full, 100% free A–Z visual course with zero sign-up wall.

Free 30-Lesson Python Course
Guide & Frequently Asked Questions

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.