Remove whitespace from the end of a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Remove whitespace from the end of a String in Python #

Use the str.rstrip() method to remove the whitespace from the end of a string, e.g. result = my_str.rstrip(). The str.rstrip() method will return a copy of the string with the trailing whitespace removed.

main.py
my_str = ' one two ' # ✅ Remove whitespace from the end of a string result = my_str.rstrip() print(repr(result)) # 👉️ ' one two' # ------------------------------------------------- # ✅ Remove trailing whitespace from each string in list my_list = [' a ', ' b ', ' c '] new_list = [item.rstrip() for item in my_list] print(new_list) # 👉️ [' a', ' b', ' c'] # ------------------------------------------------- # ✅ Remove leading whitespace from string result = my_str.lstrip() print(repr(result)) # 👉️ 'one two ' # ------------------------------------------------- # ✅ Remove leading and trailing whitespace from string result = my_str.strip() print(repr(result)) # 👉️ 'one two'

We used the str.rstrip() method to remove the whitespace from the end of a string.

The str.rstrip method returns a copy of the string with the trailing whitespace removed.

main.py
my_str = ' hello world ' result = my_str.rstrip() print(repr(result)) # 👉️ ' hello world'

The method doesn't change the original string, it returns a new string. Strings are immutable in Python.

The str.rstrip() method takes care of removing spaces, tabs and newlines from the end of the string.
main.py
my_str = ' hello world \t\n ' result = my_str.rstrip() print(repr(result)) # 👉️ ' hello world'

To remove the trailing whitespace from each string in a list:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.rstrip() method to remove the trailing whitespace from each string.
  3. The strings in the new list won't contain any trailing whitespace.
main.py
my_list = [' a ', ' b ', ' c '] new_list = [item.rstrip() for item in my_list] print(new_list) # 👉️ [' a', ' b', ' c']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str.rstrip() method to remove the trailing whitespace from the current string.

If you need to remove the leading whitespace from a string, use the str.lstrip() method.

main.py
my_str = ' one two ' result = my_str.lstrip() print(repr(result)) # 👉️ 'one two '

The str.lstrip method returns a copy of the string with the leading whitespace removed.

If you need to remove the leading and trailing whitespace from a string, use the str.strip() method.

main.py
my_str = ' one two ' result = my_str.strip() print(repr(result)) # 👉️ 'one two'

The str.strip method returns a copy of the string with the leading and trailing whitespace removed.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.