Borislav Hadzhiev
Last updated: Jul 8, 2022
Check out my new book
To remove multiple spaces from a string:
str.split()
method to split the string on each whitespace
character.str.join()
method to join the list of strings with a space.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.
str.split()
method is called without a separator, it considers consecutive whitespace characters as a single separator.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.
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.
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 +
.
+
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.