AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Overview of Python Substrings

PARTAGEZ

In the Python programming language, several ways to create « substrings » or substrings or to check the occurrences of substrings in a string are proposed.

What is a substring?

A substring is normally only nothing more than an element of a Python string. “Strings” are character strings of any size. If only part of such a string interests you and you extract it, you will obtain what is called a « substring » or sub-string in French. A simple example is the string « Hello World! » « , which you can for example split into two substrings « Hello » and « World! » « . Note that the substrings are not necessarily individual words. Each letter and character in a string can also be a separate substring.

If you are considering using Python to complete a web project, Deploy Now from IONOS may be an attractive option. By implementing an automated GitHub workflow, you can deploy and build your web projects with ease.

Create a Python substring

Slicing

If you want to write Python code that creates a substring for you, then you can turn to Python’s most popular feature of « Slicing ». This slicing function allows you to specify where your substring should start and where it should end using indexes. You pass the indexes in square brackets, separated by a colon. The start index is inclusive, while the end index is exclusive. The slicing syntax in Python therefore looks like this:

A concrete code example illustrates the slicing function:

s = "Python est un langage de programmation populaire."
résultat = s[0:6]

python

We start by creating a string named “s”. The slicing can be seen in the second line of the sample code. After the string name are specified in square brackets first the start index 0 which is followed by a colon, then the end index 6. Therefore, the substring of « s », which consists of character zero through the fifth character inclusive, is stored in the variable named « result ». In our case, the “result” variable contained the word “Python”.

If you want to cut a string from the beginning or the end, you can shorten the Python slicing notation : if you do not specify a start index, index 0 is selected by default. If you omit the ending index though, slicing will create a Python substring that extends to the end of the initial string.

If you want to start extracting substrings in your initial Python string from the end, you can use a shorthand notation that uses negative indexes :

s = "Python est un langage de programmation populaire."
résultat = s[-24:]

python

In the sample code above, the variable named « result » contains the last 24 characters of the original string « s ». Thus, the string “popular programming” appears in the “result” variable.

string methods

Besides slicing in Python, a number of predefined String methods allows you to extract a Python substring from a string.

The slice function

As the name suggests, the slice function works like slicing in Python. The syntax of the slice function is also strongly reminiscent of the already mentioned slicing. It adopts a start index and an end index and returns the corresponding substring:

string.slice(début, fin)

python

The substring function

There substring method is also great for extracting Python substrings. Syntactically, it resembles the slice function. The behavior is also analogous:

string.substring(début, fin)

python

The split function

If you’re interested in multiple Python substrings instead of just one, you should look into Python split. The function allows you to split a string into substrings using a passed separator symbol in a Python list. The syntax is not complicated:

string.split(séparateur)

python

Again, let’s use a short code example to better understand the split function procedure:

s = "Python est un langage de programmation populaire."
résultat = s.split(" ")

python

The call to the split function in Python accepts the separator as an argument: this corresponds to a space in our case. A list of all space-separated Python substrings in the original string is now stored in the « result » variable:  » [‘Python’, ‘est’, ‘un’, ‘langage’, ‘de’, ‘programmation’, ‘populaire’.’] »

Substrings resulting from regular expressions

You can also register Python Substrings in a list if you apply a regular expression to a string, then use the findall function from the “re” library. Regular expressions in Python are a format for processing strings that finds specific patterns in strings. The findall function takes a regular expression as the first transfer parameter and a string as the second parameter. A code example illustrates the procedure:

import re
s = "Python est un langage de programmation populaire."
résultat = re.findall(r"\w+", s)

python

The “re” library is imported in the first line of code so that the findall function can be accessed afterwards. The regular expression “r“\w+“” as well as the string already known in the variable named “s” are then passed to it. The regular expression may seem strange at first sight, but it expresses nothing but the need to extract all the words from a string. Thus, we are not surprised to find a list of Python substrings in the variable named “result”: “ [‘Python’, ‘est’, ‘un’, ‘langage’, ‘de’, ‘programmation’, ‘populaire’.’] « .

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact