Remove multiple spaces from a string in Python

avatar

Borislav Hadzhiev

Last updated: Jul 8, 2022

banner

Check out my new book

Remove multiple spaces from a string in Python #

To remove multiple spaces from a string:

  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 new string will not contain multiple spaces next to one another.
main.py
import re my_str = 'one two three four' # ✅ replace multiple whitespace characters with single space result = " ".join(my_str.split()) print(result) # 👉️ 'one two three four' print(repr(result)) # 👉️ 'one two three four' # --------------------------------------- # ✅ replace multiple spaces with single space result_2 = re.sub(' +', ' ', my_str) print(result_2) # 👉️ 'one two three four' print(repr(result_2)) # 👉️ 'one two three four'

The first example uses the str.split() and str.join() methods to remove multiple spaces from a string.

The str.split() method splits the original 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 = 'one two three four' # 👇️ ['one', 'two', 'three', 'four'] 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 = 'one two three four' # 👇️ ['one', 'two', 'three', 'four'] print(my_str.split()) result = " ".join(my_str.split()) print(result) # 👉️ 'one two three four' # 👇️ 'one two three four' print(' '.join(['one', 'two', 'three', 'four']))

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 remove multiple spaces from a string, 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 = 'one two three four' result = re.sub(' +', ' ', my_str) print(result) # 👉️ 'one two three four' print(repr(result)) # 👉️ 'one two three four'

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.