Python Trim Whitespace: How To Trim String Whitespace In Python Using strip(), lstrip(), and rstrip()

In Python and other programming languages, to trim whitespace is to remove any extra blank or empty space around a word or character in a string. In Python, we use the function strip() and its younger siblings lstrip() and rstrip() to trim whitespace in a String. We usually work with strip(), lstrip(), rstrip() when dealing with user input because it is nearly impossible to predict what a user will type. Consider this simple piece of code below as an example:

name = input("Name Please: ")
print(f'Hello {name}, what can I do for you?')

And the output

Name Please:
>>>     Steve
Hello     Steve     , what can I do for you?

When the user (Steve) entered his name, he included some whitespaces both at the start and at the end of his name. The effect of that is shown in the last output when we greet him back. It’s not looking really pretty, is it?
Now, dealing with user input is one example where trimming whitespace in python can be of great help, but there are a lot of cases where we have to use the three trimming functions in order to make our code work the way we intend.
Think about when you have to compare strings together. In the eyes of the user, the Strings " Steve " and "Steve" may appear to be the same.; but to Python, whitespaces are taken as string characters by themselves. So technically, " SteveΒ  Β " and "Steve" are not the same! but you may have thought they were. That’ll be a bug.
In this post, I will talk about how to trim whitespace in a Python string using the methods strip(), lstrip(), and rstrip(). Let’s get started.

Table of Contents

Python Trim Whitespace: The 3 Methods

In Python, there are three generally used methods to trim whitespace, and each of these has its own function – strip() is used to trim leading and trailing whitespace in a Python string which just means to trim whitespace from both the beginning and end of a string. Previously I said the remaining two were ‘younger siblings’ to the strip() methods because they only perform limited trimming. lstrip() trims whitespace from the beginning of a Python string (the ‘l’ here represents left) while rstrip() trims whitespace from the end of a Python string (the ‘r’ stands for right). In the next sections, I will go in-depth into each of these methods’ uses with examples.

Python Trim Whitespace How To Trim String Whitespace In Python Using strip(), lstrip(), and rstrip()

Trim leading and trailing whitespace in a Python string using strip()

Using the strip() function, you can remove trailing and leading whitespace from a string in Python. It is the standard method to clean up extra whitespace from a Python string.

➊>>> name = '  Steve  ' 
βž‹>>> name
'  Steve  '
➌>>> name = name.strip()
➍>>> name
'Steve'

At ➊, the value of the variable name is a string with leading and trailing whitespace. If you request the value of the name variable from Python at βž‹, you can see the whitespace at both ends of the string.

At ➌, we apply the strip() method to the value and we reassign its variable(of the same name) so that when we request the value of the variable name from Python again at ➍, we will get a string with trimmed whitespace from both ends.

Apart from removing leading and trailing whitespace from a string, the strip() method also takes in arguments. The most common usage, the one in the above example, is to leave it without any arguments – which removes whitespace; But if you pass some arguments, like letters that are in the string, only the outermost letters will be trimmed off the string. Let’s take a look at the example below:

➊>>> name = '  eve  '
>>> name.strip('e')
'  eve  '
βž‹>>> name2 = 'eve'
>>> name2.strip('e')
'v'
➌>>> name3 = 'steve'
>>> name3.strip('e')
'stev'

A little explanation:

At ➊, the value of the variable name starts and ends with whitespace, that’s why nothing happens when we pass ‘e’ as the character to remove. But at βž‹, this works because name2 ends and starts with the letter ‘e’. At ➌, strip('e') removes the ‘e’ at the end of the string.

Trim trailing whitespace in a Python string using lstrip()

lstrip() trims whitespace from the beginning of a Python string. Sometimes known as trailing whitespace. The ‘l’ at the beginning stands for ‘left’.

➊>>> name = '  Steve  ' 
βž‹>>> name
'  Steve  '
➌>>> name = name.lstrip()
➍>>> name
'Steve  '

A string including both leading and trailing whitespace is the value of the name variable at ➊. You can see the whitespace on both ends of the string if you query Python’s value of the name variable at βž‹.

When we reassign the value’s variable at ➌, we use Python’s lstrip() method to remove all whitespace from the string’s left side before requesting it at ➍. This will result in a string without any trailing whitespace.

The lstrip() function also takes in arguments. If the character(s) you have passed in as arguments exist at the beginning of the string, they will be removed.

>>> name = 'eve'
>>> name.lstrip('e')
've'

Trim leading whitespace in a Python string using rstrip()

With Python’s rstrip() function, excess whitespace at the end of a string can be removed. Sometimes known as the leading whitespace. The initial “r” denotes the word “right.”

➊>>> name = '  Steve  ' 
βž‹>>> name
'  Steve  '
➌>>> name = name.rstrip()
➍>>> name
'  Steve'

At ➊, name’s value is a string that contains whitespace at the beginning and end. By querying Python for the value of the name variable at βž‹, you can see the trailing and leading whitespace in the string.

When we request the value of the variable name from Python again at ➍, we will get a string with whitespace trimmed from the right side only because we applied the rstrip() method to it at➌.

Also, you can pass arguments to the rstrip() function. If the argument character or characters are found at the string’s beginning, they will be removed.

>>> name = 'eve'
>>> name.rstrip('e')
'ev'

Conclusion: Python Trim Whitespace

As I’ve said above, we usually use the trimming functions to clean up user data before we use it in the program. However, unless you know you only want to trim the right side of the string (using rstip()) or the left side of the string (using lstrip()), you will probably find yourself reaching for strip() more often than its “younger siblings.” Therefore it is a best practice to always use the strip() method to clean up your Python strings from both ends.

That’s all for this post. Thank you so much for stopping by; I will see you in other Codinggear blog posts.

Stephen Mclin
Stephen Mclin

Hey, I'm Steve; I write about Python and Django as if I'm teaching myself. CodingGear is sort of like my learning notes, but for all of us. Hope you'll love the content!

Articles: 125