Ways To Remove Whitespaces From The String In Python
Introduction
If you have learned about Python strings then you would know that -
Python String is immutable i.e., it cannot be changed. When we try to manipulate a string using any functions or methods, it returns the modified string and not the original string and we work on that modified string in our program.
In this tutorial, we are going to discuss some of the ways to remove whitespace characters from our string using various methods and functions.
We are going to see the following methods:
str.strip()
str.replace()
str.split()
str.lstrip() and str.rstrip()
re.sub()
Let’s get started and see the above-mentioned functions one by one.
strip()
The first thing that clicked in our mind to remove whitespace characters is to use the str.strip()
function.
It removes the leading and trailing whitespace characters which means it removes the whitespace from the beginning and the end of the string.
my_str = " G e e k P y t h o n. "
output = my_str.strip()
print(f"Removed whitespaces using strip: {output}")
Output
Removed whitespaces using strip: G e e k P y t h o n.
strip()
method removed the leading and the trailing whitespace from the my_str
.
replace()
Python replace()
function removes all the whitespace characters from the string including whitespace between words too.
my_str = " G e e k P y t h o n. "
output = my_str.replace(" ", "")
print(f"Removed whitespaces using replace: {output}")
Output
Removed whitespaces using replace: GeekPython.
Here, we replaced all the spaces in the string with no space, and hence, all the whitespace was removed successfully.
split()
We can use split()
function to remove duplicate whitespace characters from the string but we have to take help from join()
function.
my_str = " G e e k P y t h o n. "
output = " ".join(my_str.split())
print(f"Removed whitespaces using split: {output}")
Output
Removed whitespaces using split: G e e k P y t h o n.
lstrip() & rstrip()
If you recall the str.strip()
method, it removes leading and trailing whitespace from the string.
lstrip()
and rstrip()
- just think of them like the strip() method is divided into two parts.
lstrip()
- removes the leading whitespace characters from the string.
rstrip()
- removes the trailing whitespace characters from the string.
my_str = " G e e k P y t h o n. "
output = my_str.lstrip()
print(f"Using l-strip method: {output}")
output1 = my_str.rstrip()
print(f"Using r-strip method: {output1}")
Output
Using l-strip method: G e e k P y t h o n.
Using r-strip method: G e e k P y t h o n.
Using Regex
We can use a regular expression to remove whitespace by matching the whitespace characters and then substitute using re.sub()
method.
The syntax will be simple
my_str = re.sub(r”\s+”, “”, sentence)
Here, "r"
in the beginning, means that the string should be treated as "raw string".
And "\s+"
means that the whitespace character with one or more occurrences.
Let’s see some examples
To remove all the whitespace characters
import re
my_str = " G e e k P y t h o n. "
# Removed all the whitespaces
output = re.sub(r"\s+", "", my_str)
print(f"Removed all the whitespaces: {output}")
Output
Removed all the whitespaces: GeekPython.
Here, we substituted all the whitespace with no space.
To remove whitespace from the BEGINNING
import re
my_str = " G e e k P y t h o n. "
# Removed whitespace from the start
output1 = re.sub(r"^\s+", "", my_str)
print(f"Removed whitespace from the start: {output1}")
Output
Removed whitespace from the start: G e e k P y t h o n.
Here, we used "^\s+"
regex pattern to remove whitespace from the beginning of the string.
To remove whitespace from the END
import re
my_str = " G e e k P y t h o n. "
# Removed whitespace from the end
output2 = re.sub(r"\s+$", "", my_str)
print(f"Removed whitespace from the end: {output2}")
Output
Removed whitespace from the end: G e e k P y t h o n.
Here, we used "\s+$"
regex pattern to remove whitespace from the end of the string.
Remove whitespaces both in the BEGINNING and at the END of a string
import re
my_str = " G e e k P y t h o n. "
# Remove spaces both in the BEGINNING and at the END of a string:
output3 = re.sub(r"^\s+|\s+$", "", my_str)
print(f"Removed spaces both in the BEGINNING and at the END of a string: {output3}")
Output
Removed spaces both in the BEGINNING and at the END of a string: G e e k P y t h o n.
Here, we used "^\s+|\s+$"
regex pattern to remove whitespace from the beginning and from the end of the string.
To remove DUPLICATE spaces
import re
my_str = " G e e k P y t h o n. "
# Remove DUPLICATE spaces:
output4 = " ".join(re.split("\s+", my_str))
print(f"Removed DUPLICATE spaces: {output4}")
Output
Removed DUPLICATE spaces: G e e k P y t h o n.
If you recall using, .join()
method with str.split()
- here, we are using the same method instead we used re.split()
function.
Conclusion
You’ve learned some of the ways to clean up your Python string and apply them when you were working on your project where you work on textual data.
Out of the above-discussed ways, you can prefer any method based on your requirement and comfort.
If you want to learn about the above-discussed functions in deep then you can visit the official documentation of Python string methods.
That’s all for now.
Keep Coding✌✌
Originally published at https://geekpython.in.