You are currently viewing Remove Whitespaces from the String – Exploring Various Methods

Remove Whitespaces from the String – Exploring Various Methods

Introduction

If you have learned about Python string then you would know that –

Python String is immutable i.e., it cannot be changed. When we try to manipulate a string using any functions or methods, it returns the modified string and not the original string and we work on that modified string in our program.

In this tutorial, we are going to discuss some of the ways to remove whitespace characters from our string using various methods and functions.

We are going to see the following methods:

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

Let’s get started and see the above-mentioned functions one by one.

strip()

The first thing that clicked in our mind to remove whitespace characters is to use the str.strip() function.

It removes the leading and trailing whitespace characters which means it removes the whitespace from the beginning and the end of the string.

Output

strip() method removed the leading and the trailing whitespace from the my_str.

replace()

Python replace() function removes all the whitespace characters from the string including whitespace between words too.

Output

Here, we replaced all the spaces in the string with no space, and hence, all the whitespace was removed successfully.

split()

We can use split() function to remove duplicate whitespace characters from the string but we have to take help from join() function.

Output

lstrip() & rstrip()

If you recall the str.strip() method, it removes leading and trailing whitespace from the string.

lstrip() and rstrip() – just think of them like the strip() method is divided into two parts.

lstrip() – removes the leading whitespace characters from the string.

rstrip() – removes the trailing whitespace characters from the string.

Output

Using regex

We can use a regular expression to remove whitespace by matching the whitespace characters and then substitute using re.sub() method.

The syntax will be simple

import re

my_str = re.sub(r”\s+”, “”, sentence)

Here, "r" in the beginning, means that the string should be treated as “raw string”.

And "\s+" means that the whitespace character with one or more occurrences.

Let’s see some examples

To remove all the whitespace characters

Output

Here, we substituted all the whitespace with no space.

To remove whitespace from the BEGINNING

Output

Here, we used "^\s+" regex pattern to remove whitespace from the beginning of the string.

To remove whitespace from the END

Output

Here, we used "\s+$" regex pattern to remove whitespace from the end of the string.

Remove whitespaces both in the BEGINNING and at the END of a string

Output

Here, we used "^\s+|\s+$" regex pattern to remove whitespace from the beginning and from the end of the string.

To remove DUPLICATE spaces

Output

If you recall using, .join() method with str.split() – here, we are using same method instead we used re.split() function.

Conclusion

You’ve learned some of the ways to clean up your Python string and apply them when you were working on your project where you work on textual data.

Out of the above-discussed ways, you can prefer any method based on your requirement and comfort.

If you want to learn about the above-discussed functions in deep then you can visit the official documentation of Python string methods.


That’s all for now.

Keep Coding✌✌