How to remove whitespace from a string in python
In Python, there are various approaches to remove whitespaces in a string. By using the following methods, let's see how to remove whitespaces in a string .- str.strip()
- str.lstrip()
- str.rstrip()
- str.replace()
- translate()
- re.sub()
Python strip() method
If you want to remove leading and ending spaces in a string, use strip():
>>> str = " Python remove whitespace "
>>> print(str.strip())
Python remove whitespace
Python lstrip() method
If you want to remove spaces in the beginning of a string , use lstrip():
Python rstrip() method
If you want to remove spaces in the end of a string , use rstrip():
All three string functions strip lstrip, and rstrip can take parameters of the string to strip, with the default being all white space.
Python replace() method
If you want to remove all space characters , use replace():
Python translate() method
If you want to remove all Whitespaces includes space, tabs, and CRLF. So an elegant and one-liner string function you can use is translate():
>>> str = " Python translate() method "
>>> print(str.translate(str.maketrans('', '', ' \n\t\r')))
Pythontranslate()method
OR if you want to remove only whitespace :
import string
>>> str = " Python translate() method "
>>> print(str.translate(str.maketrans('', '', string.whitespace)))
Pythontranslate()method
Using Regular Expressions
If you want to remove leading and ending spaces in a string, use strip():
If you want to remove spaces in the beginning of a string , use lstrip():

If you want to remove spaces in the end of a string , use rstrip():
import re
str = " Python remove whitespace "
str = re.sub(r"\s+$", "", str, flags=re.UNICODE)
print(str)
If you want to remove all spaces in a string, even between words:
import re
str = " Python remove whitespace "
str = re.sub(r"\s+", "", str, flags=re.UNICODE)
print(str)
Remove all duplicate whitespaces in the string
If you want to remove all the duplicate whitespaces and newline characters, then you can use join() function with string split() function.- split(): Returns list of all words in the string separated using delimiter string. If the delimiter is not mentioned, by default whitespace is the delimiter.
- join(): This method takes all items in the iterable and combines them into a string using a separator.
import re
str = " Python remove whitespace "
str = " ".join(re.split("\s+", str, flags=re.UNICODE))
print(str)
Or

Related Topics
- How to use Date and Time in Python
- Python Exception Handling
- How to Generate a Random Number in Python
- How to pause execution of program in Python
- How do I parse XML in Python?
- How to read and write a CSV files with Python
- Threads and Threading in Python
- Python Multithreaded Programming
- Python range() function
- How to Convert a Python String to int
- Python filter() Function
- Difference between range() and xrange() in Python
- How to print without newline in Python?
- How to get the current time in Python
- Slicing in Python
- Create a nested directory without exception | Python
- How to measure time taken between lines of code in python?
- How to concatenate two lists in Python
- How to Find the Mode in a List Using Python
- Difference Between Static and Class Methods in Python?