{ }codelper

Reference › Python

Python Slicing: [start:stop:step] Explained

Slicing is the syntax for cutting one continuous piece out of a string or list and making a new one from it. You write start:stop inside square brackets, and the value at the stop index is not included. Leaving a side empty means from the beginning or to the end.

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

start:stop — the stop is not included

s[1:4] is the characters at indexes 1, 2 and 3. The length is stop - start. Leave out the start to cut from the beginning, and leave out the stop to cut to the end.

s = "abcdefg"print(s[1:4])print(s[:3])print(s[3:])print(s[:3] + s[3:])
Output
bcd
abc
defg
abcdefg

Edit and run it →

Because the stop is left out, s[:3] and s[3:] split the string into exactly two parts, with nothing overlapping and nothing missing.

Negative indexes: counting from the end

-1 is the last item and -2 is the second from the end. Even without knowing the length, you can easily cut the last few items or everything except the last one.

s = "abcdefg"print(s[-3:])print(s[:-1])print(s[-4:-1])name = "photo.png"print(name[:-4], name[-3:])
Output
efg
abcdef
def
photo png

Edit and run it →

Step: skipping and reversing

Write one more colon and then the step. [::2] takes every other item, and a negative step goes from the back to the front. [::-1] is the whole thing reversed.

s = "abcdefg"print(s[::2])print(s[1::2])print(s[::-1])print(s[5:1:-1])print(s[1:6:2])
Output
aceg
bdf
gfedcba
fedc
bdf

Edit and run it →

When the step is negative, the start must be larger than the stop. s[5:1:-1] goes backwards from index 5 to index 2.

Slicing and copying lists

Lists are sliced in exactly the same way. What you cut out is a new list, so changing it leaves the original list as it was. [:] goes from the beginning to the end, which means a copy of the whole list.

nums = [10, 20, 30, 40, 50]part = nums[1:4]part[0] = 99print(part)print(nums) copy = nums[:]copy.append(60)print(len(nums), len(copy))
Output
[99, 30, 40]
[10, 20, 30, 40, 50]
5 6

Edit and run it →

Replacing a whole piece (lists only)

Put a slice on the left of = and that piece is swapped out for other values. The number of values can be different, and you can also delete a piece with del.

a = [1, 2, 3, 4, 5]a[1:3] = [20, 30, 35]print(a)a[:2] = []print(a)del a[-2:]print(a)
Output
[1, 20, 30, 35, 4, 5]
[30, 35, 4, 5]
[30, 35]

Edit and run it →

Common mistakes

Expecting an error when you go out of range

s = "abc"
print(repr(s[1:100]))
print(repr(s[5:]))
print(s[5])

→ s[1:100] quietly gives 'bc' and s[5:] gives ''. Only s[5], which reads a single index, raises IndexError: string index out of range

Slicing cuts as far as there is something to cut and never raises an error. That is convenient, but wrong indexes slip through too. If you get an empty result, check your start and stop values first.

Slicing backwards without a step

s = "abcdefg"
print(repr(s[4:1]))
print(repr(s[4:1:-1]))

→ s[4:1] is the empty string ''

The default step is 1 (forwards), so if the start comes after the stop, nothing is selected. To go backwards, add -1.

Taking the last n items with [-n:] when n is 0

nums = [1, 2, 3, 4]
for n in [2, 1, 0]:
    print(n, nums[-n:])

→ When n is 0 you get the whole list, not an empty list

-0 is the same as 0, so it becomes nums[0:]. If n can be 0, write nums[len(nums) - n:].

Trying to replace a piece of a string

s = "hello"
s[0:1] = "J"

→ TypeError: 'str' object does not support item assignment

A string can't be changed. Slice it and join the pieces into a new string: s = "J" + s[1:].

Practice problems

See also