You are reading solutions
alfie-grace-headshot-square2.jpg
Author: Alfie Grace
Data Scientist

Quickly Substring a String in Python

You can quickly slice a string using the indexing operators [ and ] as shown below:

string = 'dheaxkalmlprloeob'
string[2:15:2]
Out:
'example'

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:

python-substring-example.png

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.

string_indexes.png
example_string = 'example_string'
example_string[0:7]
Out:
'example'

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:

example_string = 'example_string'
example_string[8:]
Out:
'string'

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 'string'.

Here's another example of this:

string = 'drop this keep this'
string[10:]
Out:
'keep this'

Option 2: [:end]

Similarly to [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 end index.

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):

example_string = 'example_string'
example_string[:7]
Out:
'example'

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 [:end] technique:

string = 'keep this drop this'
string[:9]
Out:
'keep this'

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:

url = 'learndatasci.com'
url[:-4]
Out:
'learndatasci'

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 example_string below:

example_string = 'example_string'
example_string[0:7:2]
Out:
'eape'

As the output shows, a step size of 2 has skipped every second character. In this example, using start and 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 'x', 'm' and 'l', resulting in the substring 'eape'.

The following example shows how we can use a step of -1 to reverse a string:

string = 'sdrawkcab eb ot desu gnirts siht'
string[::-1]
Out:
'this string used to be backwards'

In this example, we never entered any values for start or 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:

string = 'sdrawkcab eb ot desu gnirts siht'
string[::-2]
Out:
'ti tigue ob akad'

Summary

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], [start:], [:end] and [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.

Take the internet's best data science courses Learn More

Meet the Authors

alfie-grace-headshot-square2.jpg

Alfie graduated with a Master's degree in Mechanical Engineering from University College London. He's currently working as a top-rated data scientist on Upwork. Find him on LinkedIn.

Brendan Martin
Editor: Brendan Martin
Founder of LearnDataSci

Get updates in your inbox

Join over 7,500 data science learners.