Python Remove Whitespace From String

Python Remove Whitespace From String | The extra spaces in the string are referred to as whitespaces. Python allows a method to remove these whitespaces. Lets us go through different methods to remove spaces in the string python. Also See:- Remove Vowels from String in Python

We will see these below Python program examples:–

  1. Python remove whitespace from string
  2. Python remove whitespace from beginning and end of string
  3. Remove extra spaces from string python
  4. Python program to remove all spaces in a string

Python Remove Whitespace From String

To remove any whitespaces in a string we usually use the strip() method which is a method available in the python library to remove extra spaces.

str = "     Hii, Welcome to Know Program      "


Hii, Welcome to Know Program

In the code, we have taken str as a string, and if you observe we have white spaces in starting and in the end as well. So strip() removes these whitespaces, prints the string with the proper spaces.

Python Remove Whitespace from Beginning And End of String

The stip() method removes all the white spaces present but to remove only at the beginning and at the end we use the other two methods, let us discuss them in this section.  This code can also be referred to for how to remove leading and trailing spaces from a string in python. The word leading refers to the beginning and the word trailing refers to the end. 

First, let us see how to remove leading space, that is to remove whitespace at the beginning

Str = "     Welcome to Know Program    "


Welcome to Know Program        

In the code, we have used a method called lstrip() which removes only spaces on the left side. 

Now, let us see the code to remove the whitespace at the end of the string. 

Str = "     Welcome to Know Program    "


     Welcome to Know Program

The rstrip() method removes whitespaces at the end of the string.

Remove Extra Spaces from String in Python

Sometimes, there might be an extra space in between the string which might be unwanted, in these cases we use split() and join() methods to remove these unwanted spaces.

Program description:- Python Program to Remove Multiple Spaces in a String

Str = "Welcome      to know     program"
result = " ".join(Str.split())


Welcome to know program

We can also do this by using regular expressions.

import re
str = "Python       program"
print(re.sub(' + ',' ', str))


Python program

Python Remove All Spaces in a String

Now, will see a single code to remove all the white spaces in a string. That is it removes all the spaces in the string. 

import re
str = " Sample code to remove all     the spaces    "
res = re.sub(r"\s+", "", str)



If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *