Statement

slicing [start:stop:step]

Slicing takes a part of a list, string or tuple with [start:stop:step]: the start is included, the stop is not, and step skips items.

BeginnerEvery example below runs in your browser. Change the code and press Run.

sequence[start:stop] sequence[start:stop:step]

A slice copies out a range of items. text[0:5] gives the characters at positions 0, 1, 2, 3 and 4: the start is included, the stop is not. Positions start at 0.

Every part is optional. Leave out the start to begin at the front, leave out the stop to go to the end, so items[:3] is the first three and items[3:] is everything after them. Negative numbers count from the end: items[-2:] is the last two. A step of -1 walks backwards, which makes text[::-1] a reversed copy.

Unlike a single index, a slice never raises IndexError: if you ask for more than exists, you just get what is there. A slice of a list is a new list, so changing it does not change the original.

Parameters

ParameterWhat it is
start
optional, default beginning
Position of the first item to include. Negative counts from the end.
stop
optional, default end
Position to stop before; this item is not included.
step
optional, default 1
How far to jump each time. 2 takes every second item; a negative step goes backwards.

Return value

A new object of the same type (list, string or tuple) holding the selected items.

Examples

1Start and stop

Pythoneditable
letters = ["a", "b", "c", "d", "e", "f"]
print(letters[1:4])
print(letters[:2])
print(letters[4:])
Output
['b', 'c', 'd']
['a', 'b']
['e', 'f']

2Negative positions

Pythoneditable
word = "notebook"
print(word[-4:])
print(word[:-4])
print(word[-1])
Output
book
note
k

3Steps and reversing

Pythoneditable
nums = list(range(10))
print(nums[::2])
print(nums[1::3])
print(nums[::-1])
print("stressed"[::-1])
Output
[0, 2, 4, 6, 8]
[1, 4, 7]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
desserts

4Real data: dates and file names

Pythoneditable
stamp = "2026-09-14T08:30"
print("year:", stamp[:4])
print("month:", stamp[5:7])
print("time:", stamp[11:])
filename = "report_final.pdf"
print(filename[:-4])
Output
year: 2026
month: 09
time: 08:30
report_final

For file names, os.path.splitext() or pathlib.Path(...).stem are safer when the extension length varies.

5Copy a list and replace a part

[:] makes a shallow copy. Assigning to a slice replaces that part of a list in place.

Pythoneditable
original = [1, 2, 3, 4, 5]
copy = original[:]
copy[0] = 99
print(original)
print(copy)
original[1:3] = ["two", "three"]
print(original)
Output
[1, 2, 3, 4, 5]
[99, 2, 3, 4, 5]
[1, 'two', 'three', 4, 5]

Common mistakes

Expecting the stop item to be included

items[1:3] gives two items, positions 1 and 2. A handy rule: stop - start is how many items you get.

Pythoneditable
days = ["Mon", "Tue", "Wed", "Thu"]
print(days[1:3])
print(len(days[1:3]))
Output
['Tue', 'Wed']
2

Out-of-range: slice vs index

A slice past the end quietly returns less; an index past the end raises an error.

Pythoneditable
short = [1, 2]
print(short[0:10])
try:
    print(short[10])
except IndexError:
    print("IndexError: position 10 does not exist")
Output
[1, 2]
IndexError: position 10 does not exist

Thinking a slice copy is deep

items[:] copies the outer list only. Lists inside it are still shared; use copy.deepcopy() for a full copy.

Questions people ask

How do I get the last N items of a list?

items[-N:]. For the last three: items[-3:].

How do I reverse a string in Python?

text[::-1]. For lists you can also use items.reverse() (in place) or reversed().

Can I slice a dictionary or a set?

No: they have no positions. Convert first, for example list(d.items())[:3].

What is slice()?

slice(1, 4) builds a slice object you can store and reuse: items[s] is the same as items[1:4].

Spotted a mistake on this page? Tell us