Remove leading and trailing whitespace from String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Remove leading and trailing whitespace from String in Python #

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

main.py
my_str = ' apple banana ' # ✅ Remove leading and trailing whitespace from string result = my_str.strip() print(repr(result)) # 👉️ 'apple banana' # ----------------------------------------- # ✅ Remove leading and trailing whitespace from each string in list my_list = [' apple ', ' banana ', ' kiwi '] new_list = [item.strip() for item in my_list] print(new_list) # 👉️ ['apple', 'banana', 'kiwi'] # ----------------------------------------- # ✅ Remove leading whitespace from string result = my_str.lstrip() print(repr(result)) # 👉️ 'apple banana ' # ----------------------------------------- # ✅ Remove trailing whitespace from string result = my_str.rstrip() print(repr(result)) # 👉️ ' apple banana'

We used the str.strip() method to remove the leading and trailing whitespace from a string.

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

main.py
my_str = ' apple banana ' result = my_str.strip() print(repr(result)) # 👉️ 'apple banana'

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

The str.strip() method takes care of removing any leading and trailing spaces, tabs and newlines.
main.py
my_str = ' \n\t apple banana \t\n ' result = my_str.strip() print(repr(result)) # 👉️ 'apple banana'

To remove the leading and trailing whitespace from a list of strings:

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

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.strip() method to remove the leading and trailing whitespace from the current string.

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

main.py
my_str = ' apple banana ' result = my_str.lstrip() print(repr(result)) # 👉️ 'apple banana '

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

If you only need to remove the trailing whitespace from a string, use the str.rstrip() method.

main.py
my_str = ' apple banana ' result = my_str.rstrip() print(repr(result)) # 👉️ ' apple banana'

The str.rstrip method returns a copy of the string with the 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.