How to Remove Whitespaces in Python Strings?

An instructive image demonstrating how to use Python's string strip functions to clean up text data. Explore whitespace removal, left stripping, and right stripping

In Python, the leading and trailing spaces can be trimmed by using the built-in functions as described below:

  1. Python strip method – removes spaces from the left and right of the string and returns the copy of the string.
  2. lstrip method – returns the string after removing leading whitespaces.
  3. rstrip method – returns a string after removing the trailing whitespace – the space towards the right of the string.

See the following section explaining each method with examples.

The strip() method

The strip method removes the spaces on both sides of the string (leading and trailing) and returns a copy of that string.

 Syntax of Python strip method

This is how you may use the strip method:

str.strip([chars])
  • The strip method takes an optional parameter.
  • If chars is not given then leading and trailing spaces will be removed.
  • If provided, the strip method will remove matched characters from the given string.
  • The return value of the strip method is the copy of the string after removing the spaces or given set of characters.

An example of strip method without chars parameter

In this example, no parameter value is passed to the strip method, so it will remove any leading and trailing spaces in the given string. See the code and output below:

#Demo of using strip method

str_a = "      Python is Outstanding      "

str_b = str_a.strip()



print("The Original String:", str_a)

print("With strip method:", str_b)

Output:

Python strip

A few things to be noted in the example:

  • stra_a variable contains leading and trailing spaces
  • str_b variable is assigned the returned result of the strip method
  • str_b contains the str_a without spaces
  • The original string remains the same after using the strip method.

A demo of using parameter in the strip method

As mentioned earlier, if chars parameter is passed, the strip method removes the given set of characters in the string. See the example below:

str_a = "Python Strip Tutorial"

str_b = str_a.strip('Python')



print("The Original String:", str_a)

print("With strip method:", str_b)

Output:

strip argument

Note that, it does not specify the prefix or suffix but a combination of its values is striped.

An example of lstrip method

The lstrip() method removes the leading spaces from the specified string and returns the copy. The lstrip method takes an argument just like the strip() method i.e.

str.lstrip([chars])

See an example of the lstrip method below:

>>> '   Python lstrip method   '.lstrip()

'Python lstrip method   '

You can see, only the spaces from the left side are removed while trailing spaces still exist.

A demo of using Python rstrip() method

The rstrip() method removes the trailing spaces from the given string and returns a copy after removing whitespaces from the right side. Just like strip() and lstrip(), it also takes an argument:

str.rstrip([chars])

The following example shows how it removes the trailing spaces.

The source string contains spaces towards both sides. See the resultant string after using the rstrip method:

>>> '   Python rstrip method   '.rstrip()

'   Python rstrip method'

The strip method also trims newline (\n), \t characters

If your source string contains leading or trailing newline (\n), return (\r), or tab (\t) characters, these are also considered whitespaces and will be trimmed while using the strip(), lstrip(), or rstrip() methods.

In the following example, a string contains the leading and trailing newline and tab characters.

The string is displayed before and after using the strip() method; have a look:

#Demo of using strip with newline and tab

str_src = "\t \n \t A string with tab and newline\t \n "
str_strip = str_src.strip()

print("Original string:", str_src)
print("String after trimmed:", str_strip)

Result:

trimmed

You can see, the strip method trimmed newline and tab characters from both sides.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!