How to Remove Whitespace From Python String | 5 Examples (strip, rstrip & lstrip)
Raw text data is often not properly formatted and contains a lot of redundant whitespaces at the beginning and end of strings as well as double blank characters within the text.
In the following tutorial, I’ll explain how to remove such spaces from a string in Python. Let’s first have a look at the possible scenarios:
As you can see in the graphic, we can remove spaces at the beginning and end (see Example 1); only at the end (see Example 2); only at the beginning (see Example 3); duplicate spaces (see Example 4); and all spaces (see Example 5).
So if you want to learn more about the removal of whitespace, keep reading…
Example 1: Remove Whitespace From Beginning and End of String in Python (strip Function)
Consider the following example string in Python (i.e. Python 3):
my_string = " This sentence contains many redundant whitespaces !!! "
Our example string contains a sentence with whitespaces at the beginning and at the end (as well as double spaces within the string, but more on that later).
In order to trim all spaces before and after our sentence, we can use the strip function in Python:
my_string_1 = my_string.strip() # Apply strip function print(my_string_1) # Print updated string # "This sentence contains many redundant whitespaces !!!"
Easy breezy! So what if we want to remove only the spaces at the right or left side of our string? That’s what I’m going to show you in Example 2 and 3!
Example 2: Trim Whitespace at the Right Side of a String (rstrip Function)
With the Python strip function, we were able to delete all left and right spaces (as shown in Example 1). However, sometimes you might want to keep the whitespace at the beginning and remove only the space at the end.
For this task, we can use the rstrip Python function:
my_string_2 = my_string.rstrip() # Apply rstrip function print(my_string_2) # Print updated string # " This sentence contains many redundant whitespaces !!!"
Have a look at the quotes at the end of our sentence: We removed all trailing spaces, but kept the spaces at the beginning.
So, can we do that the other way around? Of cause we can…
Example 3: Delete Spaces at the Left Side of a String (lstrip Function)
In order to remove leading space in front of a sentence, we can use the lstrip command (exactly as we did before with rstrip):
my_string_3 = my_string.lstrip() # Apply lstrip function print(my_string_3) # Print updated string # "This sentence contains many redundant whitespaces !!! "
That’s it! But stay with me, we still need to remove the redundant spaces within the text…
Example 4: How to Remove Extra Space Between Text in Python (re.sub Operation)
So far, we have used only functions of the strip-family. However, to get rid of duplicate blank characters between the words of our sentence we need to apply the re.sub operation:
import re # Import regular expressions my_string_4 = re.sub(" +", " ",my_string) # Apply sub function print(my_string_4) # Print updated string # " This sentence contains many redundant whitespaces !!! "
You want to extract even more whitespace?! So be it…
Example 5: Eliminate All Whitespace (replace Function)
In order to remove all spaces of our text, we can apply the replace function:
my_string_5 = my_string.replace(" ", "") # Apply replace function print(my_string_5) # Print updated string # "Thissentencecontainsmanyredundantwhitespaces!!!"
That doesn’t make much sense in our specific case though 😉
Video: Additional Whitespace Examples & How to Strip Punctuation in Python
Do you need a few more examples for Python’s strip function (see also Example 1-3 of this tutorial)? Then have a look at the following YouTube video of Ethan Weed. In the video, he is giving more examples for the strip function and also explains how to strip punctuation. The examples are shown live in the Python programming software.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Further Reading