AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Python split: how to split Python strings

PARTAGEZ

Web hosting with personal advisor!

Powerful, flexible and high-performance web hosting with mailbox, personal advisor and domain included 1time year !

Domain
SSL certificate
24/7 Support

What is Python split?

In some cases, it may be useful, or even necessary, to split a Python string. The proper method is Python split. Splitting is usually performed on spaces in a character string and can be managed using different parameters. Python split is used in different forms, each adapted according to the application.

Python split syntax and how it works

The basic syntax of Python split is:

string.split(separator, maxsplit)

separator and maxsplit are the two parameters that can be used optionally. In the absence of a specified parameter, Python split splits the string corresponding to each individual space. The separator parameter specifies the string to split. The corresponding splitting is performed at each of its occurrences. You can use maxsplit to specify the frequency of splitting the chain. If you do not specify this parameter, the program will perform an infinite number of splits.

Simple examples of Python split with separator

These three simple examples illustrate how a string can be split with Python split. You will find the basic code structure in a Python tutorial.

Split at spaces

In the first code, the string is normally split at spaces. The corresponding code is as follows:

text = "Ceci est un exemple"
print(text.split())

The corresponding output looks like this:

["Ceci", "est", "un", "exemple"]

Split at commas

In the second example, you split the string at the commas. Here is the appropriate command:

animaux = "chien, chat, souris, cheval"
print(animaux.split(", " ))

The following output is displayed:

["chien", "chat", "souris", "cheval"]

Split at the sharp

You can also use a hash mark to separate terms from each other for splitting. You assign the value x to Python split. Here is the corresponding code:

farbstring = "bleu#rouge#jaune#orange"
couleurs = farb_string.split("#")
print (couleurs)

The output then looks like this:

["bleu", "rouge", "jaune", "orange"]

Set maxsplit for Python split

The second maxsplit parameter works similarly. These three examples illustrate how it works:

max split by 0

In the first example, we set maxsplit to 0:

couleurs = "bleu, rouge, jaune, orange"
print(couleurs.split(", ", 0))

The result then looks like this:

["bleu, rouge, jaune, orange"]

max split by 1

If you set maxsplit to 1, the result will be changed. Here is the code:

couleurs = "bleu, rouge, jaune, orange"
print(couleurs.split(", ", 1))

Here is the output after Python split:

["bleu", "rouge, jaune, orange"]

max split by 2

For a double split, set maxsplit to 2 and run Python split. Here is the relevant code:

couleurs = "bleu, rouge, jaune, orange"
print(couleurs.split(", ", 2))

The result is the following:

["bleu", "rouge", "jaune, orange"]

Python split within isolated words

If the default variant of a split is operated on commas or spaces, you can also split character strings within specific words. In the following example, we take our animal names and split them based on the letter has. It works as follows:

animaux = "chien, chat, souris, cheval"
print(animaux.split("a"))

The output looks unusual, but works as expected:

["chien, ch", "t, souris, chev", "l"]

If you add the maxsplit parameter and limit the number of fractions to 1, the following result appears:

animaux = "chien, chat, souris, cheval"
print(animaux.split("a", 1))
["chien, ch", "t, souris, cheval"]

Convert string to list with Python split

A popular and often very useful feature of Python split is converting a string to a Python list. You must use the two parameters separator and maxsplit for this purpose. In the following example, our list of colors is split at the hash level and organized by maxsplit into a list of at most two elements. For this, we have defined maxsplit with the value 1. The corresponding code is as follows:

farb_string = "bleu#rouge#jaune#orange"
couleurs = farb_string.split("#", 1)
print(couleurs)

We get the following output:

["bleu", "rouge#jaune#orange"]

The difference between Python split and rsplit

Besides Python split, the rsplit method is also available to you. It is similar to the variant shown here and effectively returns the exact same result in many applications. In particular, if the optional maxsplit parameter is not specified, the output is identical. However, if you set the separator parameter to None, leaving it empty, and if you set maxsplit to 2you will see a difference:

animaux = "chien, chat, souris, cheval"
print(animaux.split(None, 2))

Here is the output with Python split:

["chien", "chat", "souris, cheval"]

Python split thus splits the string from the left. If you now use rsplit with the following code:

animaux = "chien, chat, souris, cheval"
print(animaux.rsplit(None, 2))

you get this output:

["chien, chat", "souris", "cheval"]

Splitting with rsplit is therefore exercised on the right.

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