Split a string by one or more spaces in Python

avatar

Borislav Hadzhiev

Last updated: Jun 25, 2022

banner

Photo from Unsplash

Split a string by one or more spaces in Python #

Use the str.split() method without an argument to split a string by one or more spaces, e.g. my_str.split(). When the str.split() method is called without an argument, it considers consecutive whitespace characters as a single separator.

main.py
my_str = 'alice bob carl diana' my_list = my_str.split() print(my_list) # 👉️ ['alice', 'bob', 'carl', 'diana']

We used the str.split() method to split a string by one or more spaces.

The str.split() method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator (optional)
maxsplitAt most maxsplit splits are done (optional)
When the str.split() method is called without a separator, it considers consecutive whitespace characters as a single separator.

If the string starts or ends with a trailing whitespace, the list won't contain empty string elements.

main.py
my_str = ' alice bob carl diana ' my_list = my_str.split() print(my_list) # 👉️ ['alice', 'bob', 'carl', 'diana']
If we don't pass an argument to the split() method and split an empty string or one that only contains whitespace characters, we'd get an empty list.
main.py
my_str = ' ' my_list = my_str.split() print(my_list) # 👉️ []

You can also use a regular expression to split a string by one or more spaces.

Use the re.split() method to split a string by one or more spaces, e.g. re.split(r'\s+', my_str). The re.split() method will split the string on each occurrence of one or more spaces and will return a list containing the results.

main.py
import re my_str = 'alice bob carl diana' my_list = re.split(r'\s+', my_str) print(my_list) # 👉️ ['alice', 'bob', 'carl', 'diana']

The re.split method takes a pattern and a string and splits the string on each occurrence of the pattern.

The \s character matches unicode whitespace characters like [ \t\n\r\f\v].

The plus + is used to match the preceding character (whitespace) 1 or more times.

In its entirety, the regular expression matches one or more whitespace characters.

When using this approach, you would get empty string elements if your string starts with or ends with a whitespace.

main.py
import re my_str = ' alice bob carl diana ' my_list = re.split(r'\s+', my_str) print(my_list) # 👉️ ['', 'alice', 'bob', 'carl', 'diana', '']

You can use the filter() function to remove any empty strings from the list.

main.py
import re my_str = ' alice bob carl diana ' my_list = list(filter(None, re.split(r'\s+', my_str))) print(my_list) # 👉️ ['alice', 'bob', 'carl', 'diana']

The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

If you pass None for the function argument, all falsy elements of the iterable are removed.

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 0 (zero) of any numeric type
  • empty sequences and collections: "" (empty string), () (empty tuple), [] (empty list), {} (empty dictionary), set() (empty set), range(0) (empty range).

Note that the filter() function returns a filter object, so we have to use the list() class to convert the filter object to a list.

You can also use the re.split() method to split a string by 1 or more occurrences of a different delimiter.

Here is an example that splits a string by 1 or more occurrences of an underscore.

main.py
import re my_str = 'alice__bob_carl________diana' my_list = re.split(r'_+', my_str) print(my_list) # 👉️ ['alice', 'bob', 'carl', 'diana']

The plus + is used to match the preceding character (underscore) 1 or more times.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.