Python string slicing extracts part of a string with the syntax text[start:stop:step]. The slice begins at index start, stops before index stop, and moves by step characters. Any of the three parts can be left out, and the result is always a new string.
Slicing works the same way on lists, tuples and other sequences, so learning it for strings pays off everywhere. The examples use Python 3.13 and 3.14.
Every character in a string has a position, called an index. Counting starts at 0 from the left. Negative numbers count from the right, where -1 is the last character.
Reading a single character with an index outside the string raises IndexError. Slices behave differently, as shown below.
The basic form is text[start:stop]. The start index is included and the stop index is excluded.
Leaving out start means the beginning, and leaving out stop means the end. A useful rule is that text[:n] gives the first n characters, and the length of text[a:b] is b - a when both are inside the string.
The third value is the step, which sets how far to move each time:
Negative values are useful when you care about the end of a string:
A negative step walks backwards. The common idiom for reversing a string is a step of -1:
With a negative step the defaults swap, so start defaults to the end and stop to the beginning. For example, "abcdef"[4:1:-1] returns "edc".
An out-of-range slice is clipped to the string rather than failing:
The second print shows an empty line, because the result is an empty string. This makes slices safe for tasks like taking "at most the first 80 characters" with text[:80]. A step of 0 is the exception and raises ValueError.
Strings are immutable, so you cannot assign into a slice:
Build a new string instead:
Each slice copies the selected characters into a new string. For very large strings, repeated slicing in a loop can be expensive, so consider iterating or using str.find() and str.partition() when that matters.
The syntax text[a:b:c] is shorthand for text[slice(a, b, c)]. Naming a slice makes fixed-width parsing readable:
More everyday uses:
| Task | Code | Result for "hello world" |
|---|---|---|
| First word if length is known | s[:5] | hello |
| Last three characters | s[-3:] | rld |
| Every other character | s[::2] | hlowrd |
| Drop first and last | s[1:-1] | ello worl |
| Check palindrome | s == s[::-1] | False |
Slicing counts characters (Unicode code points), not bytes, so "héllo"[1] is "é". Some visible characters, such as emoji with modifiers, are made of several code points and can be split by a slice.
Half-open ranges make lengths easy to calculate and let slices join cleanly, for example s[:3] + s[3:] is always the original string.
It returns the string reversed, because the step is -1 and both start and stop are left at their defaults.
No. Strings are immutable. Create a new string by joining slices with other text, or convert to a list of characters, change it and join it back.
Python clips them and returns whatever overlaps, possibly an empty string. Only a single-index lookup such as text[10] raises IndexError.
Use text[start:stop:step] to take part of a string: start is included, stop is excluded, and negative values count from the end. Omitted parts use sensible defaults, out-of-range slices are clipped, and every slice returns a new string.