Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
  Signup/Sign In

How to remove all whitespaces from the string

In a python programming language, whitespace characters are nothing but spaces, tabs, or newlines. These characters can be seen which may be given in before or after some characters in the programming language.

In this tutorial, we will learn how to remove whitespaces from the string using the strip(), replace(), split() and join() methods.

Example: Using strip() Method

The below example shows how to remove the whitespace from the string using strip() methods.

#Initializing string.
#Remove the whitespaces using strip() methods.
string="\tPython is very easy\t"
print("The string is:",string)
x=string.lstrip()
y=string.rstrip()
z=string.strip()
print("The string after removing spaces:",x)
print("The string after removing spaces:",y)
print("The string after removing spaces:",z)

In the above example, First, we initialize the string by giving the tabs(\t) at both sides of the string.

In code line 5, 6, and 7, we use the lstrip(), rstrip(), and strip() method to remove the spaces at the left side that is leading whitespace, at the right side that is trailing whitespace, and spaces at both sides respectively.

Once we run the program, we will get the following output.


The string is: Python is very easy
The string after removing spaces: Python is very easy
The string after removing spaces: Python is very easy
The string after removing spaces: Python is very easy

Example: The replace() Method

The below example shows how to remove whitespaces from the string using the replace() method.

#Initializing string.
#Remove the whitespaces using replace() methods.
string="\tPython is very easy\t"
print("The string is:",string)
x=string.replace("\t","")
print("The string after removing spaces:",x)

In the above example, First, we initialize the string by giving the tabs(\t) at both sides of the string.

Next, we apply the replace() method, which replaces the old char with the specified new char. In the example, we are replacing the tab spaces with the empty separator.

Once we run the program, we will get the following output.


The string is: Python is very easy
The string after removing spaces: Python is very easy

Example: The join() and strip() Method

The below example shows how to remove spaces from the string using the join() method.

#Initializing string
string_1="\tPython is very easy\t"
print("The string 1 is:",string_1)
string_2=string_1.split()
print("The string 2 is:",string_2)
x=" ".join(string_2)
print("The string after removing spaces:",x)

In the above example, first, we initialize the string. In the next step, we are using the split method which splits the given string with a space separator and returns the list that will be stored in the variable string_2

In the next step, we are passing that list as a parameter to the join() function.

Next, we are joining the elements in the list with the space separator using the join() method. A separator can be anything.


The string 1 is: Python is very easy
The string 2 is: ['Python', 'is', 'very', 'easy']
The string after removing spaces: Python is very easy

Conclusion

In this tutorial, we learned how to remove the leading whitespace and trailing whitespaces from the string using the strip() methods, replace() method, and join() method.