Replace multiple spaces with a single space in Python

avatar

Borislav Hadzhiev

Last updated: Jul 8, 2022

banner

Photo from Unsplash

Replace multiple spaces with a single space in Python #

To replace multiple spaces with a single space:

  1. Use the str.split() method to split the string on each whitespace character.
  2. Use the str.join() method to join the list of strings with a space.
  3. The words in the new string will be separated by a single space.
main.py
import re my_str = 'avocado banana kiwi apricot' # ✅ replace multiple whitespace characters with single space result = " ".join(my_str.split()) print(repr(result)) # 👉️ 'avocado banana kiwi apricot' # ---------------------------------------------- # ✅ replace multiple spaces with single space result_2 = re.sub(' +', ' ', my_str) print(repr(result_2)) # 👉️ 'avocado banana kiwi apricot'

The first example uses the str.split() and str.join() methods to replace multiple whitespace characters with a single space.

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

When the str.split() method is called without a separator, it considers consecutive whitespace characters as a single separator.
main.py
my_str = 'avocado banana kiwi apricot' # 👇️ ['avocado', 'banana', 'kiwi', 'apricot'] print(my_str.split())

When called without an argument, the str.split() method splits on consecutive whitespace characters (e.g. \t, \n, etc), not only spaces.

The next step is to use the str.join() method to join the list of strings with a space separator.

main.py
my_str = 'avocado banana kiwi apricot' result = " ".join(my_str.split()) print(repr(result)) # 👉️ 'avocado banana kiwi apricot' # 👇️ 'avocado banana kiwi apricot' print(' '.join(['avocado', 'banana', 'kiwi', 'apricot']))

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

An alternative approach is to use the re.sub() method.

Use the re.sub() method to replace multiple spaces with a single space, e.g. result = re.sub(' +', ' ', my_str). The re.sub method will return a new string that is obtained by replacing all occurrences of multiple spaces with a single space.

main.py
import re my_str = 'avocado banana kiwi apricot' result = re.sub(' +', ' ', my_str) print(repr(result)) # 👉️ 'avocado banana kiwi apricot'

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

If the pattern isn't found, the string is returned as is.

The first argument we passed to the re.sub method is a regular expression.

In our regex, we have a space and a plus +.

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

In it's entirety, the example replaces 1 or more consecutive spaces with a single space.

Note that the re.sub() method returns a new string, it doesn't mutate the original string as strings are immutable in Python.

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.