How to Replace Multiple Spaces with a Single Space in Python
This guide explains how to replace multiple spaces (and other whitespace characters) within a string with a single space in Python. We'll cover the most efficient and readable methods using built-in string functions (split()
and join()
) and regular expressions (re.sub()
). We will also learn how to apply the same operation when dealing with files.
Using split()
and join()
(Recommended)
The most Pythonic and often the most efficient way to replace multiple spaces with a single space is to combine str.split()
and str.join()
:
my_str = 'tutorial reference com'
result = " ".join(my_str.split())
print(repr(result)) # Output: 'tutorial reference com'
-
my_str.split()
: When called with no arguments,split()
does the following:- It splits the string at any whitespace character (space, tab, newline, etc.).
- It treats consecutive whitespace characters as a single separator.
- It automatically removes leading and trailing whitespace.
- It returns a list of words.
-
" ".join(...)
: This joins the list of words back into a single string, using a single space (" "
) as the separator.
This two-step process effectively removes all extra whitespace, leaving only single spaces between words. It's concise, readable, and generally very fast.
Using re.sub()
(Regular Expressions)
The re.sub()
function (from the re
module for regular expressions) provides a more powerful, but slightly less readable, way to do this:
import re
my_str = 'tutorial reference com'
result = re.sub(r'\s+', ' ', my_str) # Match 1 or more whitespace chars
print(repr(result)) # Output: 'tutorial reference com'
# If there might be leading/trailing spaces:
result = re.sub(r'\s+', ' ', my_str.strip())
print(repr(result))
r'\s+'
: This is a regular expression.\s
: Matches any whitespace character (space, tab, newline, etc.).+
: Matches one or more occurrences of the preceding character (so it matches one or more spaces, tabs, newlines, etc.).
re.sub(r'\s+', ' ', my_str)
: Replaces every occurrence of one or more whitespace characters with a single space.- The
strip()
method can be chained to remove any trailing whitespace at the end of the string.
Replacing Multiple Spaces in a File
To process an entire file and replace multiple spaces with single spaces:
with open('file-1.txt', 'r', encoding='utf-8') as old_file:
with open('file-2.txt', 'w', encoding='utf-8') as new_file:
for line in old_file:
new_line = ' '.join(line.split()) + '\n' # Add newline back
new_file.write(new_line)
- The
with
statement makes sure the file is properly closed. - The outer loop iterates over the lines in the file.
- This code reads the input file line by line, replaces multiple spaces with single spaces in each line, and writes the modified lines to a new file. The
\n
adds a newline character to the end of each processed line to preserve the original line structure.