Skip to content
Theory · Strings · lesson 3 of 10

Slices

In the first lesson of this theme you took one tile at a time off the rack. But a program often needs a whole piece of a string: the year from a date, or an area code.

Let's go back to the rack with the word. Picture marks drawn between the tiles, each mark with its own number. The mark before the first tile is 0, the next one is 1, and so on up to the mark after the last tile. To take a piece, you name two marks, and everything between them is your piece.

In Python, the two bounds of a piece go in square brackets, separated by a colon:

Python
date = "2026-10-14"
year = date[0:4]
month = date[5:7]
print(year, month)  # 2026 10
print(date[8:])  # 14

Let's go through the example line by line:

  1. date[0:4] is the characters between marks 0 and 4, that is, with indices from 0 to 3: the year 2026.
  2. date[5:7] is the characters with indices 5 and 6: the month 10.
  3. date[8:] runs from index 8 to the end of the string: the day 14.

Start and stop

The expression s[start:stop] is called a slice. It returns a new string of the characters with indices from start to stop, and stop is not included, as in range. On the rack you can see why: mark stop stands before the tile with the same number, so that tile stays out.

While both bounds are non-negative and inside the string, and start is at most stop, the length of a slice is stop - start. For date[0:4] that is four characters.

A missing bound

Either bound can be left out:

  • without start, the slice begins at the first character;
  • without stop, the slice runs to the end of the string.

Bounds can be negative too, like indices:

Python
s = "abcdef"
print(s[:3], s[3:], s[-2:])  # abc def ef
print(s[2:100], s[10:20] == "")  # cdef True

What the first line shows:

  • s[:3] is the first three characters, s[3:] is all the rest, and s[-2:] is the last two;
  • slices that share a bound fit together with nothing lost: s[:3] + s[3:] always equals s.

A bound past the end

Unlike an index, a bound past the end raises no error: the slice cuts it down to the length of the string. So s[2:100] returns the characters up to the end, and s[10:20] returns an empty string. Meanwhile s[100] still raises an IndexError.

Step

The third number in the slice s[start:stop:step] is the step: the slice takes every step-th character. On the rack, it is like taking every other tile. A step can be negative too:

  • s[::2] is every second character from the start;
  • s[1::2] is every second one, starting at index 1;
  • s[::-1]: a negative step goes from right to left, and the string comes out backward.
Python
s = "abcdef"
print(s[::2], s[1::2])  # ace bdf
print(s[::-1])  # fedcba
print(s[4:1:-1])  # edc

With a negative step, start stands to the right of stop. The slice s[4:1:-1] takes indices 4, 3 and 2, and 1 is again not included.

Here the picture with marks stops working: between marks 4 and 1 stand the characters with indices 1, 2 and 3. With a negative step, count by the indices of the characters themselves: from start included up to stop, not including it.

Without bounds, a slice with step -1 goes through the whole string, from the last character to the first. Interviewers like to ask this: "How do you reverse a string in Python?" The answer is s[::-1].

The original string does not change: a slice always returns a new string. The tiles stay on the rack, and you get copies of them.

Common mistake

The last three characters are taken with a slice that ends in -1:

Python
code = "ID-2048"
last = code[-3:-1]  # ✗
print(last)  # 04

There is no error, but the result is 04 instead of 048. The stop bound is not included in the slice, so index -1, the last character, is dropped. A slice reaches the end of the string without stop: code[-3:] gives 048.

Key points

  • s[start:stop] takes the characters from start to stop, not including stop.
  • A missing bound means the start or the end of the string, and a bound past the end raises no error.
  • s[::step] takes every step-th character, and s[::-1] reverses the string.
  • A slice returns a new string and does not change the original.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way