Last updated: Apr 9, 2024
Reading timeยท4 min
To replace multiple spaces with a single space:
str.split()
method to split the string on each whitespace
character.str.join()
method to join the list of strings with a space.my_str = 'bobby hadz com' # โ replace multiple whitespace characters with single space result = " ".join(my_str.split()) print(repr(result)) # ๐๏ธ 'bobby hadz com'
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.
str.split()
method is called without a separator, it considers consecutive whitespace characters as a single separator.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.
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.
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.
import re my_str = 'bobby hadz com' # ๐๏ธ only covers spaces result = re.sub(' +', ' ', my_str) print(repr(result)) # ๐๏ธ 'bobby hadz com'
If you need to replace multiple whitespace characters with a single space, use the following code sample instead.
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 +
.
result = re.sub(' +', ' ', my_str)
+
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.
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()
.
import re my_str = 'bobby hadz com ' result = re.sub(r'\s+', ' ', my_str.strip()) print(repr(result)) # ๐๏ธ 'bobby hadz com'
The str.strip() method returns a copy of the string with the leading and trailing whitespace removed.
To replace multiple spaces with a single space in a file:
split()
and join()
methods to replace multiple spaces with a
single space.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 sample assumes that you have a file named file-1.txt
located in the
same directory as your Python script.
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.
bobby hadz com one two three
You can learn more about the related topics by checking out the following tutorials: