How to create and edit Python substrings

In Python, there are different ways to create substrings or to check for substrings within a string.

What is a Python substring?

A substring is essentially just a portion of a Python string, with a string being a sequence of characters of any length. A popular example of a string is ‘Hello World!’. If you’re only interested in a part of the string, for example, just the word ‘Hello’ or just the word ‘World!’, you can extract it from the string. The word that is extracted is called a substring. Substrings don’t have to be whole words though. Every letter or character in a string can be a substring.

Tip

If you’re interested in Python because you are working on a web project, you should take a look at Deploy Now by IONOS. The automatic GitHub workflow lets you easily build and deploy web projects.

How to create Python substrings

Create Python substrings with slicing

If you want to create a substring in Python, the most well-known functionality for doing this is slicing. Slicing allows you to use indices to select where your substring starts and ends. Simply enter these indices in square brackets and separate them with a colon. The start index is inclusive, while the end index is exclusive. The syntax of slicing in Python looks like this:

string[start:end]
python

The following example should help to make slicing a bit clearer:

s = "Python is a popular language."
result = s[0:6]
python

First, we created a string called ‘s’. In the second line of the code example, you can see how slicing works. After the name of the string, there are square brackets. Inside the brackets, you’ll find the start index 0 followed by a colon and then the end index 6. The ‘result’ variable then stores a substring of ‘s’, which consists of the characters from the 0th index up to and including the 5th index. In this example, the ‘result’ variable contains the word ‘Python’.

If you want to slice a string from the beginning or from the very end, you can simply use the default values. If you don’t enter a start index, Python will default to 0. If, on the other hand, you don’t enter an end index, Python will automatically designate the final character of the string as the end point.

If you want to extract substrings starting from the end of a string, you can use negative indices.

s = "Python is a popular language."
result = s[-8:]
pythonl

The ‘result’ variable in the example above extracts the last 8 letters from the ‘s’ string, giving us the substring ‘language’.

Create Pythons substrings with different string methods

In addition to Python slicing, there are a range of predefined string methods that you can use to extract a substring from a string.

The slice function

As the name suggests, the slice function works the same as Python slicing. However, the syntax of this function is different. It takes a start and end index and delivers back the substring:

string.slice(start, end)
python

The substring function

The substring function can also be used to extract Python substrings. The syntax for this function is similar to the slice function syntax:

string.substring(start, end)
python

The split function

If you want to see multiple Python substrings instead of just one at a time, you can use Python splits. With this function, you can specify a separator to create a Python list of substrings. The syntax isn’t complicated:

string.split(separator)
python

To see exactly how this works, let’s have a look at another example:

s = "Python is a popular language."
result = s.split(" ")
python

The separator in this example is a blank space: ’ ’. This means that for every blank space, there will be a split. In the original string, there are five words written separately. Due to the blank space between the words, each word is now a substring. The substrings are stored in the ‘result’ variable: [‘Python’, ‘is’, ‘a’, ‘popular’, ‘language.’].

Create Python substrings with regular expressions

You can also save Python substrings in a list if you use a regular expression on a string and then the findall function from the ‘re’ library. Regular expressions in Python are used to identify patterns in strings as well as modify strings. The findall function takes a regular expression as the first parameter and a string for the second parameter. Here’s an example:

import re
s = "Python is a popular language."
result = re.findall(r"\w+", s)
python

The first line of code imports the library ‘re’, giving access to the findall function. Following this, the regular expression ‘r“\w+“’ and the ‘s’ string are passed to the findall function. Although the regular expression may seem strange at first glance, it simply means that all words should be extracted from the string. The ‘result’ is a list of Python substrings: [‘Python’, ‘is’, ‘a’, ‘popular’, ‘language’].

In order to provide you with the best online experience this website uses cookies. By using our website, you agree to our use of cookies. More Info.
Manage cookies