Explain Codes LogoExplain Codes Logo

How do I remove leading whitespace in Python?

python
functions
best-practices
collections
Anton ShumikhinbyAnton Shumikhin·Sep 9, 2024
TLDR

Quickly remove leading whitespace with lstrip() in Python:

remodeled_str = " text with leading spaces".lstrip() # Results in "text with leading spaces"

Need to go all out? strip() pounces on both ends:

remodeled_str = " On both sides ".strip() # Results in "On both sides"

Let's even declutter multiline strings with some list comprehension finesse:

remodeled_lines = "\n".join(line.lstrip() for line in " line 1\n line 2".splitlines()) # Multiline magic

Keep your text strings neat and tidy with these swift techniques.

Customizing the whitespaces to remove

In case you choose the minimalistic approach, lstrip(' ') can target only spaces:

remodeled_str = " Spaces, not tabs! \t".lstrip(' ') # Results in "\tSpaces, not tabs!"

Remember, motherships can accept combinations ☺️:

remodeled_str = " \t\n Spaceships ".lstrip(' \t\n') # Spaceships are left floating in the end

Taming whitespace tigers with regex

Unleash the regex armada for ultimate control and customization, lead the re.sub() charge:

import re remodeled_str = re.sub("^\s+", "", " text walking a whitespace tightrope") # The text is no longer terrified

Want your regex to pull specific stunts? Regex are your circus performers:

remodeled_str = re.sub("^[^\w]+", "", " text 123") # Here the ^\w means non-alphanumeric characters so 123 stays

Wrestling with multiline strings using textwrap

Get chummy with textwrap.dedent() when dealing with multiline strings:

import textwrap multiline_string = """ Some indented text Some more indented text""" remodeled_str = textwrap.dedent(multiline_string).strip() # Indents stripped, text liberated!

Making adjustments to cope with indentation is the secret trick here 🔑

Ensuring whitespace doesn't escape eviction

Facing unpredictable input? Fear not! strip() to the rescue:

clean_input = user_input.strip() # Tidies up the input like a character Marie Kondo

Look after the readability of your code; using textwrap.dedent() tends to look less like an ancient spell compared to complex regex solutions.

Remember, regex patterns are powerful, yet cryptic. Comment your regexes; future you will thank past you.

# Removing leading letters that have escaped from the alphabet remodeled_str = re.sub("^[^a-zA-Z]+", "", " 123text") # Results in "text", also sends these letters back to alphabet school 😂

Choosing the right tool for the job

Here's a cheat sheet to help you:

  • Single-line with spaces? Use lstrip(' ')
  • Single-line generic whitespace? Go for lstrip()
  • Both ends, single-line? strip() is your guy
  • Erratic multiline indents? textwrap.dedent() cleans it up
  • Complex patterns? Unleash the regex monster with re.sub()