Remove extra spaces between words in a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Remove extra spaces between words in a String in Python #

To remove the extra spaces between words in a string:

  1. Use the str.split() method to split the string on one or more whitespace characters.
  2. Use the str.join() method to join the strings with a single space separator.
  3. The words in the new string will only be separated by a single space.
main.py
import re my_str = 'one two three four five' # ✅ Remove extra spaces between words (str.join() and str.split()) result = ' '.join(my_str.split()) print(repr(result)) # 👉️ 'one two three four five' # --------------------------------------------------------- # ✅ Remove extra spaces between words (using re.sub()) result = re.sub(r'\s+', ' ', my_str) print(repr(result)) # 👉️ 'one two three four five'

The first example uses the str.split() and str.join() methods to remove the extra spaces between the words in a string.

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 = 'one two three four five' # 👇️ ['one', 'two', 'three', 'four', 'five'] 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 last 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 five' result = ' '.join(my_str.split()) print(repr(result)) # 👉️ 'one two three four five' # 👇️ 'one two three four five' print(' '.join(['one', 'two', 'three', 'four', 'five']))

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.

Remove extra spaces between words in a String using re.sub() #

Use the re.sub() method to remove extra spaces between the words in a string, e.g. result = re.sub(r'\s+', ' ', 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 five' result = re.sub(r'\s+', ' ', my_str) print(repr(result)) # 👉️ 'one two three four five'

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.

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.

This helps us replace multiple whitespace characters with a single space.

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.