Quickly Substring a String in Python
You can quickly slice a string using the indexing operators
] as shown below:
In this example, we used
[start:end:step] to get a result. We use
start to define the starting index and
end to mark the endpoint.
For example, just using
string[2:15] would return 'eaxkalmlprloe', but setting
step to two tells Python to jump through the characters two at a time. The following image gives a breakdown of this process:
By slicing a string, you get what is known as a substring. See the image below for the string
example_string with indices, followed by a snippet of code you can use on
example_string to return a substring.
By defining the start and end points for the slice, we get the substring
example_substring. This substring begins at the
example_string index which matches the start point. In our case, this is index 0. The substring ends at the index that comes before the end point. In our example, the end point is 7, so the substring goes up to index 6.
Today we'll be taking a look at three other techniques you can use when slicing strings.
Option 1: [start:]
One way of slicing a string is by just defining the start point, which will return a substring that begins at the
start index and includes the rest of the string. You can see this process below using a
start value of 8:
As the output shows, our substring starts at index 8 of
example_string, which is
's'. The slice then returns the rest of the string, giving us the substring
Here's another example of this:
Option 2: [:end]
[start:], we can use
[:end], which only requires you to enter an end point with the parameter
end. Using this approach will return a substring with everything that comes before the
See below for an example of this using
example_string (note that this produces the same result as
example_string[0:7] in the intro):
Notice that the second
'e' is at index 6, so using an end point of 7, Python returns a substring containing everything that comes before. This means that the end index is non-inclusive.
The following code shows another example using the
Sometimes the substring we want is easier to create using the end of the string. In this case, we can use negative indexes to index from the other side.
For example, if we want to remove ".com" from a URL:
In this case, it's much easier to remove ".com" — which is always four characters — than it is to know the exact number of characters to index in a URL.
Option 3: [start:end:step]
By adding a third parameter,
step, to a slice operator, you can skip certain characters. By using a step size of 2, you will step over every second character.
We can show this using
As the output shows, a step size of 2 has skipped every second character. In this example, using
end values of 0 and 7 respectively gives us the substring
'example'. Adding a step size of 2 excludes every second character in our substring, dropping
'l', resulting in the substring
The following example shows how we can use a step of -1 to reverse a string:
In this example, we never entered any values for
end, so the slice included the entire string. By entering a negative value for
step, the slice operator moves through the
string variable backwards. The value for
step is -1, so the operator moves in steps of size 1.
See below for the same example but using a step size of -2. A value for
step of -2 gives the result
this string used to be backwards but skips every second character:
Using indexes to get substrings is a very similar process to list slicing, with there being various techniques we can use to slice a string. Some examples of these which we've covered are
[start:end:step]. The ability to slice strings and lists is essential for any budding Pythoneer, so it's a good idea to get into the habit of using the slice operator early on.