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.
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!'
If you're trying to remove all sorts of whitespace characters (space, tab, newline, etc.) you could use the split
and join
string 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 convert a list to a string in Python).
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'
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.
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'
What if you need to strip whitespace from the beginning and end of each line in your string?
You could split your lines with the string splitlines
method, 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'
Need to fill-in gaps in your Python skills?
Sign up for my Python newsletter where I share one of my favorite Python tips every week.
Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.