Strings II

Python String split()

In this tutorial, we will explore the Python string method str.split() which is used to split a string into multiple smaller strings.


Let's talk about the Python string method str.split().

As the name suggests, it's used to split an existing string into multiple smaller strings.

Here's a simple example:

text = 'hello world' print(text.split())
Python
Output

As you can see, the original string is split by the whitespace character, and the resulting substrings are returned in a list.

This is the default behavior of the str.split() method.

You can also define the delimiter used to split the string by passing a custom separator as argument.

str.split(separator)
Python

Let's split the string at the character o:

text = 'hello world' print(text.split('o'))
Python
Output

This time, we receive a list of three strings because the character o appears twice in the original string, causing it to split at both occurrences.

Notice that the whitespace before the w is preserved.

Only the character o has been removed, as it was the delimiter used for splitting the string.

An example use case for str.split() is to break a text into individual words or sentences.


What will be the output?

text = 'a-b-c-d' print(text.split('-'))
Python

What will be the output?

text = 'ab cd' print(text.split())
Python

What will be the output?

text = 'a b c d' print(text.split('b'))
Python

Good job!

Another characteristic of str.split() is that if you specify a separator that doesn't exist in the original string, the result will be a list containing the entire string as a single element.

For example, here we try to split a string using the non-existing character Z:

text = 'programming' print(text.split('Z'))
Python
Output

However, let's see what happens when we split the text at the letter m, which appears twice in a row:

text = 'programming' print(text.split('m'))
Python
Output

Since the function splits the original string at each occurence of the separator, we get three substrings. Because there's nothing between the two ms, one of the substrings is an empty string.


What will be the output?

text = 'abcd' print(text.split('e'))
Python

What will be the output?

text = 'brilliant' print(text.split('l'))
Python

Lust but not least, str.split() has a second optional parameter called maxsplit:

str.plit(separator, maxsplit)
Python

You can use it to determine the maximum number of split.

By default, there's no limit, so the string splits at every occurrence of the separator.

For example, here we limit the number of splits to 2, even though the separator - appears more than twice:

text = '1-2-3-4' print(text.split('-', 2))
Python
Output

What will be the output?

text = 'brilliant' print(text.split('i', 1))
Python