Replace multiple spaces with a single space in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Replace multiple spaces with a single space in Python
  2. Replace multiple spaces with a single space using re.sub()
  3. Replace multiple spaces with a single space in a File in Python

# 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
my_str = 'bobby hadz com' # โœ… replace multiple whitespace characters with single space result = " ".join(my_str.split()) print(repr(result)) # ๐Ÿ‘‰๏ธ 'bobby hadz com'

replace multiple spaces with single space

The code for this article is available on GitHub

The 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 = 'bobby hadz com' # ๐Ÿ‘‡๏ธ ['bobby', 'hadz', 'com'] 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 = 'bobby hadz com' result = " ".join(my_str.split()) print(repr(result)) # ๐Ÿ‘‰๏ธ 'bobby hadz com' # ๐Ÿ‘‡๏ธ bobby hadz com print(' '.join(['bobby', 'hadz', 'com']))

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.

# Replace multiple spaces with a single space using re.sub()

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

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 = 'bobby hadz com' # ๐Ÿ‘‡๏ธ only covers spaces result = re.sub(' +', ' ', my_str) print(repr(result)) # ๐Ÿ‘‰๏ธ 'bobby hadz com'

replace multiple spaces with single space using re sub

The code for this article is available on GitHub

If you need to replace multiple whitespace characters with a single space, use the following code sample instead.

main.py
import re my_str = 'bobby hadz com' # ๐Ÿ‘‡๏ธ covers spaces, tabs and newline chars result = re.sub(r'\s+', ' ', my_str) print(repr(result)) # ๐Ÿ‘‰๏ธ 'bobby hadz com'

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 +.

main.py
result = re.sub(' +', ' ', my_str)
The plus + is used to match the preceding character (the space) 1 or more times.

In its 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.

If you need to replace all whitespace characters with a single space, use the \s special character instead.

main.py
import re my_str = 'bobby hadz com' result = re.sub(r'\s+', ' ', my_str) print(repr(result)) # ๐Ÿ‘‰๏ธ 'bobby hadz com'

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

If your string has trailing whitespace, you might have to use the str.strip() method to remove it before calling re.sub().

main.py
import re my_str = 'bobby hadz com ' result = re.sub(r'\s+', ' ', my_str.strip()) print(repr(result)) # ๐Ÿ‘‰๏ธ 'bobby hadz com'

using str strip before re sub

The str.strip() method returns a copy of the string with the leading and trailing whitespace removed.

# Replace multiple spaces with a single space in a file in Python

To replace multiple spaces with a single space in a file:

  1. Open the first file in reading mode.
  2. Open the second file in writing mode.
  3. Use the split() and join() methods to replace multiple spaces with a single space.
main.py
with open('file-1.txt', 'r', encoding='utf-8') as old_file: lines = old_file.readlines() with open('file-2.txt', 'w', encoding='utf-8') as new_file: for line in lines: new_file.write(' '.join(line.split()) + '\n')
The code for this article is available on GitHub

The code sample assumes that you have a file named file-1.txt located in the same directory as your Python script.

file-1.txt
bobby hadz com one two three

We opened the first file in r (reading) mode and read its lines.

Then, we opened the second file in w (writing) mode and used a for loop to iterate over the lines of the first file.

On each iteration, we replace multiple spaces with a single space and write the result to the second file.

The contents of file-2.txt look as follows.

file-2.txt
bobby hadz com one two three

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev