Python slicing takes a part of a string or list with the syntax sequence[start:stop:step]. The slice starts at start, stops just before stop, and moves step positions at a time. A negative index counts from the end, so -1 is the last item.
What is slicing in Python?
Every character in a string has a position, called an index. Indexes start at 0. In "Corner Café", the letter C is at 0, the space is at 6, and the second C (the start of Café) is at 7.
text = "Corner Café" print(text[0]) print(text[7]) print(text[0:6]) print(text[7:]) print(text[:6])
C C Corner Café Corner
text[0]is one character, the first.text[0:6]takes positions 0 to 5. Position 6 is not included.text[7:]leaves outstop, so it goes to the end.text[:6]leaves outstart, so it begins at 0.
Why does the stop index not include the last position?
The stop is where the slice ends, and it is excluded. This looks odd at first, but it has two benefits. The length of text[a:b] is always b - a, and text[:n] plus text[n:] gives the whole text with no overlap and no gap.
text = "Corner Café" print(text[3:3] == "") print(len(text), text[0:100])
True
11 Corner CaféAn empty slice, where start equals stop, gives an empty string. A stop that is too large does not cause an error. Python stops at the end.
How does negative indexing work in Python?
A negative index counts from the end: -1 is the last item, -2 is the one before it.
text = "Corner Café" print(text[-1]) print(text[-4:]) print(text[:-5])
é Café Corner
text[-4:] means "the last 4 characters". text[:-5] means "everything except the last 5". Negative indexes are handy when you do not know the length of the text.
One difference to remember: a single index that is out of range is an error, but a slice never is.
text = "Corner Café" print(text[3:100])
ner Café
What does the step do in a slice?
The third value is the step. A step of 2 takes every second item. A negative step goes backward.
text = "Corner Café" print(text[::2]) print(text[::-1]) print(text[6:0:-1])
Cre aé éfaC renroC renro
text[::-1] is the usual way to reverse a string. With a negative step, start should be larger than stop. In text[6:0:-1] the slice begins at position 6 (the space) and goes back to position 1, so position 0 is excluded as always.
Does slicing work on lists?
Yes, with the same rules. A slice of a list is a new list.
items = ["latte", "tea", "brownie", "lemonade", "water"] print(items[1:3]) print(items[-2:]) print(items[::2]) print(items[::-1]) copy = items[:] copy[0] = "mocha" print(items[0], copy[0])
['tea', 'brownie'] ['lemonade', 'water'] ['latte', 'brownie', 'water'] ['water', 'lemonade', 'brownie', 'tea', 'latte'] latte mocha
items[:] is a quick way to make a copy. Changing the copy does not change the original list.
Real examples with order data
Fixed-format text is easy to take apart with slices.
date = "2026-03-02" print(date[:4], date[5:7], date[8:]) code = "ORD-1001" print(code[4:], int(code[4:]) + 1)
2026 03 02 1001 1002
Common mistakes
- Counting the stop as included.
text[0:6]has 6 characters (positions 0 to 5), not 7. - Expecting an error on a slice that is too long. There is none. Only a single index raises
IndexError. - Using `text[11]` on an 11-character string. The last valid index is 10, or
-1. Python reportsIndexError: string index out of range. - Mixing up the direction. With a negative step,
text[0:6:-1]is empty because the slice cannot go backward from 0 to 6.
What to read next
Practice indexing and slicing in the lesson [Index and slice](/learn/python/index-slice). Then learn the methods that work on text in [Python string methods](/blog/python-string-methods), or see how lists work in [Python list methods](/blog/python-list-methods). The whole [Python course](/courses/python) has 40 lessons and is free.
- #python
- #slicing
- #indexing
- #negative index