How to remove spaces in Python

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 minute read Works on Python 3.7—3.10
Share

Need to remove spaces from your a string in Python? Let's talk about how to remove all spaces, remove duplicate spaces, remove spaces from the ends of our strings, remove trailing newlines, and remove spaces from the beginning and end of each line.

Remove spaces from a string

Need to remove all spaces from a string in Python?

If it's just space characters you could use the string replace method to replace all spaces by an empty string.

If we call the replace method on this greeting string:

>>> greeting = " Hello world! "
>>> no_spaces = greeting.replace(" ", "")

The resulting no_spaces string will have all space characters removed:

>>> no_spaces
'Helloworld!'

Remove all whitespace from a string

If you're trying to remove all sorts of whitespace characters (space, tab, newline, etc.) you could use the string split and join methods:

If we call split on this version string, Python will split on all consecutive whitespace characters:

>>> version = "\tpy 310\n"
>>> version.split()
['py', '310']

The string join method can join an iterable of strings by a delimiter (see turning a list into a string). If we join with a delimiter of empty string (""), we'll effectively remove all spaces:

>>> no_spaces = "".join(version.split())
>>> no_spaces
'py310'

If you're comfortable with regular expressions, you can also use a regular expression to replace all consecutive whitespace by an empty string:

>>> import re
>>> no_spaces = re.sub(r"\s+", r"", version)
>>> no_spaces
'py310'

Remove duplicate whitespace

What if you just need to get rid of extra spaces (collapsing consecutive spaces)?

We could use the string split and join methods, as before, but join on a space character instead of an empty string:

>>> version = "\tpy 310\n"
>>> normalized_spaces = " ".join(version.split())
>>> normalized_spaces
'py 310'

Note that this normalizes all whitespace characters (so newlines and tabs will be converted as well) and it removes spaces from the ends of our string.

Strip whitespace from the beginning and end of a string

What if you only need to remove whitespace from the beginning and end of your string?

You can use the string strip method:

>>> version = "\tpy 310\n"
>>> stripped_version = version.strip()
>>> stripped_version
'py 310'

By default the strip method removes all whitespace characters (not just spaces).

The strip method also accepts an optional argument if you prefer to strip just a specific character. It also has two cousin methods: lstrip (for splitting from the left-hand side) and rstrip (for splitting from the right-hand side).

If you just need to remove an optional trailing newline character from the end of your string, you can use strip (passing in a \n character):

>>> version = "\tpy 310\n"
>>> no_trailing_newline = version.rstrip("\n")
>>> no_trailing_newline
'\tpy 310'

Strip whitespace from the beginning and end of each line

What if you need to strip whitespace from the beginning and end of each line in your string?

You could split your lines with splitlines, use a comprehension to call the strip method on each line, and then use the join method to join your lines back together with newline characters:

>>> string = " Line 1\nLine 2  \n Line 3 \n"
>>> stripped = "\n".join([
...     line.strip()
...     for line in string.splitlines()
... ])
...
>>> stripped
'Line 1\nLine 2\nLine 3'

Although this is complex enough that I'd usually start to reach for regular expressions at this point:

>>> import re
>>> stripped = re.sub(r"^\s+|\s+$", r"", string, flags=re.MULTILINE)
>>> stripped
'Line 1\nLine 2\nLine 3'
Write more Pythonic code

Need to fill-in gaps in your Python skills? I send regular emails designed to do just that.