We can extract a substring (a part of a string) from a string by using a slice in Python
We define a slice by using square brackets, a start offset, an end offset, and an optional step count between them.
Slice String in Python
The slice will include characters from offset start to one before end:
- [:] extracts the entire sequence from start to end.
- [ start :] specifies from the start offset to the end.
- [: end ] specifies from the beginning to the end offset minus 1.
- [ start : end ] indicates from the start offset to the end offset minus 1.
- [ start : end : step ] extracts from the start offset to the end offset minus 1, skipping characters by step.
Slice string with offset in Python
Python’s slicing syntax allows you to extract a substring from a string with an offset. You can use the syntax [start:stop] to specify the start, stop. The start is 0-based, and the stop is exclusive. The offsets goes from 0, 1, and so on from the start to the right, and –1,–2, and so forth from the end to the left.
If We don’t specify start, the slice uses 0 (the beginning). If we don’t specify end, it uses the end of the string.
Let’s make a string of the lowercase English letters:
>>> letters = ‘abcdefghijklmnopqrstuvwxyz’
Using a plain : is the same as 0: (the entire string):
Here’s an example from offset 20 to the end:
Now, from offset 10 to the end:
And another, offset 12 through 14. Python does not include the end offset in the slice. The start offset is inclusive, and the end offset is exclusive:
The three last characters:
In this next example, we go from offset 18 to the fourth before the end; notice the difference from the previous example, in which starting at –3 gets the x, but ending at –3 actually stops at –4, the w:
In the following, we extract from 6 before the end to 3 before the end:
If we want a step size other than 1, specify it after a second colon, as shown in the next series of examples.
Slice string with step size in Python
The step is the number of characters to move forward between start and stop.For example, if you have a string “abcdef” and you want to extract every other character, you can use the following syntax: “abcdef”[::2]. This will return “ace”.
From the start to the end, in steps of 7 characters:
From offset 4 to 19, by 3:
From offset 19 to the end, by 4:
From the start to offset 20 by 5:
(Again, the end needs to be one more than the actual offset.)
Slice string with negative step size in Python
And that’s not all! Given a negative step size, this handy Python slicer can also step backward. This starts at the end and ends at the start, skipping nothing:
It turns out that you can get the same result by using this:
Slices are more forgiving of bad offsets than are single-index lookups with . A slice offset earlier than the beginning of a string is treated as 0, and one after the end is treated as -1, as is demonstrated in this next series of examples.
From 50 before the end to the end:
From 51 before the end to 50 before the end:
From the start to 69 after the start:
From 70 after the start to 70 after the start: