In this tutorial let’s learn about How to Remove Spaces From a String in Python.
Contents
Introduction
There are several methods for removing spaces from a Python string. The string replace() method is the most basic approach. If the spaces to be removed are only at the beginning and end of the string, the strip() function will also work. A regular expression can be used as an alternate approach.
There are several methods in Python for removing whitespaces from a string. Let’s look at how to remove whitespaces from a string using the methods listed below.
- str.strip()
- str.lstrip()
- str.rstrip()
- str.replace()
- translate()
- re.sub()
Remove all Spaces from a String using replace()
in Python
replace() is a built-in Python method that returns a copy of the string in which all occurrences of a substring are replaced with another substring. The Python string replace() function is the simplest way to remove all spaces from a string.
Syntax for replace() method:
string.replace(old_substring, new_substring, count)
Code Sample:
sample_text = " Similar Geeks Tutorial "
# removing whitespaces
modified_text = sample_text.replace(" ","")
print(modified_text)
Output:
SimilarGeeksTutorial
Remove Spaces at the Beginning of a String in Python
str.lstrip()
to Remove Space at the Beginning of the String
Use lstrip()
to eliminate spaces from the beginning of a string. The String lstrip() function in Python returns a duplicate of the string with the leading characters removed (based on the string argument passed). It removes leading spaces if no arguments are supplied.
lstrip() Syntax
string.lstrip(character)
character : A set of characters to be removed at the beginning.
string = " Similar Geeks"
# Removes spaces at the left
print(string.lstrip())
Output:
Similar Geeks
re.sub()
to Remove Space at the Beginning of the String
The re or RegEx module is a Python library that allows you to manipulate regular expressions. This package offers a function called re.sub() that can be used to replace repeating characters with another character or sub-string.
import re
sample = " Similar Geeks"
#re.sub()
new_string = re.sub(r"^\s+", "", sample)
print(new_string)
Output:
Similar Geeks
Remove Spaces at the End of a String in Python
str.rstrip()
to Remove Space at the End of a String
Use rstrip()
to eliminate spaces from the end of the string. The String rstrip() function in Python returns a duplicate of the string with the trailing characters removed (based on the string argument passed). It removes trailing spaces if no arguments are supplied.
rstrip() Syntax
string.lstrip(character)
character : A set of characters to be removed at the ending of the string.
string = "Similar Geeks "
# Removes spaces at the right
print(string.rstrip())
Output:
Similar Geeks
re.sub()
to Remove Space at the End of the String
The re or RegEx module is a Python library that allows you to manipulate regular expressions. This package offers a function called re.sub() that can be used to replace repeating characters with another character or sub-string.
import re
sample = "Similar Geeks "
#re.sub()
new_string = re.sub(r"\s+$", "", sample)
print(new_string)
Output:
Similar Geeks
Remove Spaces Both at the Beginning and End of a String in Python
str.strip() is a function that combines str.lstrip() and str.rstrip() to eliminate whitespace at the beginning and end of a string.
string = " Similar Geeks "
# Removes spaces
print(string.strip())
Output:
Similar Geeks
re.sub()
to Remove Spaces Both at the Beginning and End of the String
Code:
import re
sample = "Similar Geeks "
#re.sub()
new_string = re.sub(r"^\s+|\s+$", "", sample)
print(new_string)
Output:
Similar Geeks
Using translate() to Remove Whitespaces in a String Python
The translate() method returns a string in which each character is mapped to the character in the translation table that corresponds to it. The maketrans() method generates the translation table.
Code sample:
import string
text = " Similar Geeks Python "
print(text.translate(text.maketrans('', '', string.whitespace)))
Output:
SimilarGeeksPython
Remove the Duplicate Whitespaces in a String in Python
Code : To remove duplicate spaces using str.split() and join() methods.
string = " Similar Geeks"
# Removes duplicate spaces
new = " ".join(string.split())
print(new)
Output:
Similar Geeks
Conclusion
In this tutorial we covered all the possible ways to remove the whitespaces from the string using different Functions and Modules in python.
Similar Posts:
Convert a String to Variable Name in Python
Remove Commas From String in Python