Python – Trim Whitespace Characters from Strings

Leading or trailing whitespace characters present in strings may not necessarily be useful. In this tutorial, we will look at how to trim whitespace characters from strings in python.

Python strings come with built-in functions like lstrip(), rstrip(), and strip() to trim whitespaces based on the use case. For example, you can use –

  • The lstrip() function to trim leading whitespace characters.
  • The rstrip() function to trim trailing whitespace characters.
  • The strip() function to trim leading and the trailing whitespace characters.

Let’s look at the usage of these functions with the help of examples.

The lstrip() function is used to remove leading characters from a string and by default removes leading whitespace characters. It returns a copy of the string with the leading characters removed.

# string with leading whitespaces
s = "  \n\t  John Doe"
# show original string
print(s)
# remove leading whitespaces
print(s.lstrip())

Output:

 
	  John Doe
John Doe

You can see that the leading white spaces have been removed in the returned string. Note that, \n and \t are also considered whitespace characters in python.

Note that, you can use the lstrip() function to remove any leading characters and not just whitespace characters. For example, let’s remove all leading vowels from a string.

# string
s = "eat this burger"
# show original string
print(s)
# remove leading vowels
print(s.lstrip("aeiou"))

Output:

eat this burger
t this burger

All the leading vowels (“ea” in this case) have been removed in the returned string.

The rstrip() function is used to remove trailing characters from a string and by default removes trailing whitespace characters. It returns a copy of the string with the trailing characters removed.

# string with trailing whitespaces
s = "John Doe\n\t  "
# show original string
print(s)
# remove trailing whitespaces
print(s.rstrip())

Output:

John Doe
	  
John Doe

You can see that the trailing white spaces have been removed in the returned string.

Note that, you can use the rstrip() function to remove any trailing characters and not just whitespace characters. For example, let’s remove all trailing vowels from a string.

# string
s = "eat this burger from Rome"
# show original string
print(s)
# remove trailing vowels
print(s.rstrip("aeiou"))

Output:

eat this burger from Rome
eat this burger from Rom

All the trailing vowels (“e” in this case) have been removed in the returned string.

The strip() function is used to remove both leading and trailing characters from a string and by default removes whitespace characters. It returns a copy of the string with the leading and the trailing characters removed.

# string with trailing whitespaces
s = " \t\nJohn Doe\n\t  "
# show original string
print(s)
# remove leading and trailing whitespaces
print(s.strip())

Output:

 	
John Doe
	  
John Doe

You can see that the all the leading and the trailing white spaces have been removed in the returned string.

Note that, you can use the strip() function to remove any trailing characters and not just whitespace characters. For example, let’s remove all leading and trailing vowels from a string.

# string
s = "eat this burger from Rome"
# show original string
print(s)
# remove leanding and trailing vowels
print(s.strip("aeiou"))

Output:

eat this burger from Rome
t this burger from Rom

All the leading and trailing vowels have been removed in the returned string.

For more on the strip() function, refer to its documentation.

You might also be interested in – Python – Remove Character From String

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.