How to remove whitespaces in strings of Python?

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

  • Python strip method – removes spaces from left and right of the string and return the copy of string
  • lstrip method – return the string after removing leading whitespaces
  • rstrip method – returns a string after removing the trailing whitespace – the space towards the right of the string
A strip example  with chars argument example

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 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:

See online demo and code

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 strip method

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

See online demo and code

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:

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:

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 as 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:

See online demo and code

trimmed

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