Slicing Lists & Strings

Slicing is the syntax seq[start:stop:step] for extracting a new sub-sequence — a range of elements from a list, string, or tuple — without writing a loop.

Learn Slicing Lists & Strings in our free Python course — an interactive lesson with runnable examples, a practice exercise and a quick reference.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Once you can grab "the first three", "everything after the second", or "this reversed" in a single bracket expression, you'll reach for slicing constantly. It's one of the most distinctly Pythonic tools you'll learn.

A slice asks for everything from start up to but not including stop . The same syntax works on lists and strings:

Think of indexes as sitting between the items. letters[1:4] takes the three elements between position 1 and position 4.

Leave out start to mean "from the beginning" and leave out stop to mean "to the end". Negative numbers count back from the last item:

data[:-1] ("all but the last") and data[-2:] ("the last two") are everyday idioms — worth memorizing.

The third number is the step : how far to jump each time. A step of 2 takes every other item; a negative step walks backwards:

A slice never touches the original — seq[:] is the classic shallow-copy trick:

Put a slice on the left of = to replace that whole section — the replacement can even be a different length:

Replace each ___ so the slices produce the commented output. Think about which bound or step you need.

✅ The stop is excluded. To include index 2, use [0:3] . Remember: stop = "first index NOT taken".

✅ A single index out of range raises IndexError ; a slice out of range simply clamps to what exists.

✅ To go right-to-left you need a negative step: [1, 2, 3][::-1] gives [3, 2, 1] .

Use only slicing to pull apart a filename and a CSV row. No split() allowed!

Go deeper with the official Python documentation:

Lesson complete — you slice like a pro!

You can grab ranges with start:stop , count from the end with negatives, stride or reverse with step , copy with [:] , and rewrite sections with slice assignment. These tricks return again and again in real code.

🚀 Up next: Booleans & Truthiness — how Python decides what counts as True, and the surprising values that count as False.

Practice quiz

What is the full slice syntax?

Slicing uses square brackets with colons: seq[start:stop:step].

Given letters = ['a','b','c','d','e','f'], what is letters[1:4]?

  • b
  • c
  • d

Answer: b. Indexes 1, 2, 3 are taken; stop (4) is excluded, giving ['b','c','d'].

Why is the stop index excluded in a slice?

  • It's a bug in Python
  • Stop is always included
  • Only for strings
  • Slices are half-open: stop is the first index NOT included

Answer: Slices are half-open: stop is the first index NOT included. Slices are half-open, so the length is stop minus start and seq[:i]+seq[i:] rebuilds the original.

What does 'Python'[2:5] return?

  • 'yth'
  • 'tho'
  • 'thon'
  • 'ytho'

Answer: 'tho'. Indexes 2,3,4 of 'Python' are t, h, o, giving 'tho'.

What does data[:3] mean?

  • From the start up to (not including) index 3
  • From index 3 to the end
  • Every 3rd item
  • The last 3 items

Answer: From the start up to (not including) index 3. Omitting start defaults to 0, so data[:3] takes the first three items.

What does data[-2:] return?

  • Everything but the last two
  • The second item
  • The last two items
  • An empty list

Answer: The last two items. Negative indices count from the end; -2: gives the last two items.

What does seq[::-1] do?

  • Removes the last item
  • Returns a reversed copy of the sequence
  • Returns every other item
  • Raises an error

Answer: Returns a reversed copy of the sequence. A step of -1 walks backwards over the whole sequence, returning a reversed copy.

What does nums[::2] return for [0,1,2,3,4,5]?

A step of 2 starting at 0 takes every other item: [0,2,4].

What is seq[:] commonly used for?

  • Deleting a list
  • Making a shallow copy of the sequence
  • Reversing a list
  • Sorting

Answer: Making a shallow copy of the sequence. seq[:] produces a brand-new copy and leaves the original untouched.

What happens with nums[10] vs nums[1:10] on [1,2,3]?

  • Both raise IndexError
  • Both clamp to what exists

A single out-of-range index raises IndexError, but an out-of-range slice clamps to what exists.