Remove the leading whitespace from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 14, 2022

banner

Photo from Unsplash

Remove the leading whitespace from a String in Python #

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

main.py
from textwrap import dedent my_str = ' apple ' # ✅ remove leading whitespace result = my_str.lstrip() print(repr(result)) # 👉️ 'apple ' # ✅ remove leading and trailing whitespace result_2 = my_str.strip() print(repr(result_2)) # 👉️ 'apple' # --------------------------------------------------- # ✅ remove leading whitespace for multiline string my_multiline_str = """\ first line second line third line""" result_3 = dedent(my_multiline_str) # first line # second line # third line print(result_3)

We used the str.lstrip() method to remove the leading whitespace from a string.

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

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

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

If you need to remove the leading and trailing whitespace from the string, use the str.strip() method.
main.py
my_str = ' apple ' result_2 = my_str.strip() print(repr(result_2)) # 👉️ 'apple'

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

If you need to remove the leading whitespace from a multiline string, use the textwrap.dedent() method.

main.py
from textwrap import dedent my_multiline_str = """\ first line second line third line""" result = dedent(my_multiline_str) # first line # second line # third line print(result)

The textwrap.dedent method takes a multiline string and removes the common leading whitespace from every line of the string.

The method is used to display multiline strings that are indented in the source code without any indentation.

You can also use the inspect.cleandoc method if you want to remove the empty lines at the beginning and end of the multiline string and the leading whitespace.
main.py
from inspect import cleandoc multiline_str = """ first line second line third line """ # # first line # second line # third line # print(multiline_str) # first line # second line # third line print(cleandoc(multiline_str))

I intentionally didn't add a backslash at the end of the first line and didn't close the multiline string on the same line.

Notice that the inspect.cleandoc method removed the empty lines at the beginning and end of the string and removed the leading whitespace.

If you don't want to remove the empty lines at the beginning and end of the multiline string, use the textwrap.dedent() method.

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.