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

What is a Python substring?

A substring is es­sen­tial­ly just a portion of a Python string, with a string being a sequence of char­ac­ters of any length. A popular example of a string is “Hello World!”. If you’re only in­ter­est­ed in a part of the string, for instance, 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. Sub­strings don’t have to be whole words though. Every letter or character in a string can be a substring.

Tip

If you’re in­ter­est­ed 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 sub­strings

Create Python sub­strings with slicing

If you want to create a substring in Python, the most well-known func­tion­al­i­ty 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 char­ac­ters 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, it will default to 0. If, on the other hand, you don’t enter an end index, Python will au­to­mat­i­cal­ly designate the final character of the string as the end point.

If you want to extract sub­strings 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 sub­strings with different string methods

In addition to Python slicing, there are a range of pre­de­fined 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 sub­strings. 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 sub­strings 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 sub­strings. The syntax isn’t com­pli­cat­ed:

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 sep­a­rate­ly. Due to the blank space between the words, each word is now a substring. The sub­strings are stored in the “result” variable: [‘Python’, ‘is’, ‘a’, ‘popular’, ‘language.’].

Create Python sub­strings with regular ex­pres­sions

You can also save Python sub­strings in a list if you use a regular ex­pres­sion on a string and then the findall function from the “re” library. Regular ex­pres­sions in Python are used to identify patterns in strings as well as modify strings. The findall function takes a regular ex­pres­sion 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 ex­pres­sion “r“\w+“” and the “s” string are passed to the findall function. Although the regular ex­pres­sion 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 sub­strings: [‘Python’, ‘is’, ‘a’, ‘popular’, ‘language’].

Go to Main Menu