Remove Whitespaces from Strings in Python

Using string methods and regexes in Python

Photo by Retha Ferguson from Pexels

How to remove whitespaces in a string in python

  • str.lstrip()
  • str.rstrip()
  • str.strip()
  • str.replace()
  • str.split()
  • re.sub()

By using the above-mentioned methods, let’s see how to remove whitespaces in a string.

Topic covered in this story

Photo by Author

1. Removing leading whitespaces in a string

1. str.lstrip()
Return a copy of the string with leading whitespaces removed.

s="   Hello   Python  "
print (s.lstrip())
#Output:Hello   Python

2. re.sub()
Using re.sub(), we can remove leading whitespaces.

Syntax:

re.sub(pattern, repl, string, count=0, flags=0)

pattern – specify the pattern to be replaced
repl – replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.
count – The optional argument count is the maximum number of pattern occurrences to be replaced. If omitted or zero, all occurrences will be replaced.

pattern= r”^s+”

r Python’s raw string notation for regular expression
s matches whitespace characters.
+ It will match one or more repetitions of whitespace character.
^(Caret.) Matches the start of the string

pattern mentioned is whitespace at the beginning of the string.
s1=re.sub(pattern,””,s)

re.sub() will replace the pattern (whitespaces at the beginning of string) to empty string.

s="   Hello   Python  "
import re
pattern= r"^s+"
s1=re.sub(pattern,"",s)
print (s1)#Output:Hello   Python

2. Removing trailing whitespace in a string

Photo by Author

str.rstrip()

Return a copy of the string with trailing whitespaces removed.

s="   Hello   Python  "
print (s.rstrip())
#Output:   Hello   Python

re.sub()
Using re.sub(), we can remove trailing whitespaces.

pattern= r"s+$"

r-Python’s raw string notation for regular expression
s — matches whitespace characters.
+ It will match one or more repetitions of whitespace character.
$ Matches the end of the string

pattern mentioned are whitespaces at the end of the string.
s1=re.sub(pattern,””,s)

re.sub() will replace the pattern (whitespaces at the end of the string) to empty string.

s="   Hello   Python  "
import re
pattern= r"s+$"
s1=re.sub(pattern,"",s)
print (s1)#Output:Hello   Python

3. Removing leading and trailing whitespace in a string

Photo by Author

str.strip()

Return the copy of the string with leading whitespaces and trailing whitespaces removed.

s="   Hello   Python  "
print(s.strip())
#Output:Hello   Python

re.sub()

Using re.sub(), we can remove leading whitespaces and trailing whitespaces.
pattern="^s+|s+$"

^s+ Matches one or more whitespaces at the beginning of the string

s+$ Matches one or more whitespaces at the end of the string.

| x|y matches either x or y

s="   Hello   Python  "
import re
pattern="^s+|s+$"
s1 = re.sub(pattern, "",s)
print(s1)
#Output:Hello   Python

4. Remove all whitespaces in a string.

Photo by Author

str.replace()
Return a copy of the string with all occurrences of substring old replaced by new.

Syntax:

str.replace(old,new,count)

print (s.replace(" ",""))

It will replace all whitespace characters into an empty string.

s="   Hello   Python   "
print (s.replace(" ",""))
#Output:HelloPython

re.sub()
Using re.sub(), we can remove all whitespaces in the string.

pattern=r"s+"
s1 = re.sub(pattern, "", s)

pattern matches all whitespaces in the string. 
Then re.sub(), replacing pattern(whitespaces ) to empty string.

s="   Hello   Python   "
import re
pattern=r"s+"
s1 = re.sub(pattern, "", s)
print (s1)
#Output:HelloPython

str.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() — join () method takes all items in the iterable and combines them into a string using a separator

s1="".join(s.split())

Splitting the string and joining the elements using an empty string

s="   Hello   Python   "
print (s.split())#Output:['Hello', 'Python']
#joining the elements in the string using empty string.
s1="".join(s.split())
print (s1)#Output:HelloPython

5. Removing duplicate whitespaces in the string

  • str.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() — join () method takes all items in the iterable and combines them into a string using a separator

s1=" ".join(s.split())

Splitting the string and joining the elements using a single whitespace string

s="Hello      Python"
print (s.split())#Output:['Hello', 'Python']
#joining the elements in the string using single whitespace string.
s1=" ".join(s.split())
print (s1)#Output:Hello Python

My other blogs related to string methods

split() vs. partition() in Python Strings

5 Ways to Find the Index of a Substring in Python


Resources:

StackOverflow

lstrip()

replace()

rstrip()

strip()

split()

re.sub()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s