How to Remove Whitespace in a String

It shows you below how to remove the whitespaces from a string. It could be mainly categorized into two different approaches, one is Python str methods, like str.split() and str.replace(); the other is Python regular expression method.

We will use the string " Demo Example " as the string to be processed in the following examples.

Remove whitespace at the beginning

str.lstrip() method

>>> demo = " Demo  Example  "
>>> demo.lstrip()
"Demo  Example  "

Here, str.lstrip() method removes the leading characters specified in the method argument. If there is no argument given, it simply removes the leading whitespaces.

Python regex method

>>> import re
>>> demo = " Demo  Example  "
>>> re.sub(r"^\s+", "", demo)
"Demo  Example  "

^ forces regex only finds the matching string at its beginning, and\s means to match all different kinds of whitespaces like whitespace, tab, return, etc. or in other words, its is equal to the collection of these special characters [ \t\n\r\f\v]. + indicates it should match whitespaces as many as possible.

You could refer to this Python regular expression tutorial to learn more about regex.

Remove whitespace at the end

str.rstrip() method

In contrast to str.lstrip() that strips the characters at the beginning of the string, str.rstrip() strips the characters at the end.

>>> demo = " Demo  Example  "
>>> demo.lstrip()
" Demo  Example"

Python regex method

Similarly, you should use an expression that matches the whitespaces at the end of the string.

>>> import re
>>> demo = " Demo  Example  "
>>> re.sub(r"\s+$", "", demo)
" Demo  Example"

Remove whitespaces at both the beginning and the end

str.strip() Method

str.strip() is the combination of str.lstrip() and str.rstrip() to eliminate whitespaces at the beginning and the end of the string.

>>> demo = " Demo  Example  "
>>> demo.strip()
"Demo  Example"

Python regex sub() method

>>> import re
>>> demo = " Demo  Example  "
>>> re.sub(r"^\s+|\s+$", "", demo)
"Demo  Example"

Remove all whitespaces

Python string replace method str.replace()

It is not necessary to check the position of the white space. Therefore, you could use str.replace() method to replace all the whitespaces with the empty string.

>>> demo = " Demo  Example  "
>>> demo.replace(" ", "")
'DemoExample'

Python string regex replace sub() method

The regex expression could only be \s+ to match the whitespaces.

>>> import re
>>> demo = " Demo  Example  "
>>> re.sub(r"\s+", "", demo)
"DemoExample"

Only remove the duplicated whitespaces

Python string split method str.split()

>>> demo = " Demo  Example  "
>>> " ".join(demo.split())
'Demo Example'

str.split() returns a list of the sub-string in the string, using whitespaces as the delimiter string.

Python regex split method re.split()

>>> import re
>>> demo = " Demo  Example  "
>>> " ".join(re.split(r"\s+", demo)
" Demo Example "
Warning

The results of re.split() and str.split() are different where re.split() will have an empty string at the beginning or the end of the list if the string has whitespaces in these positions, but str.split() doesn’t include any empty string in its result.