James Wanchai
James Wanchai

Reputation: 3031

How do I remove leading whitespace in Python?

I have a text string that starts with a number of spaces, varying between 2 & 4.

What is the simplest way to remove the leading whitespace? (ie. remove everything before a certain character?)

"  Example"   -> "Example"
"  Example  " -> "Example  "
"    Example" -> "Example"

Upvotes: 264

Views: 528431

Answers (7)

robmsmt
robmsmt

Reputation: 1529

My personal favorite for any string handling is strip, split and join (in that order):

>>> ' '.join("   this is my  badly spaced     string   ! ".strip().split())
'this is my badly spaced string !'

In general it can be good to apply this for all string handling.

This does the following:

  1. First it strips - this removes leading and ending spaces.
  2. Then it splits - it does this on whitespace by default (so it'll even get tabs and newlines). The thing is that this returns a list.
  3. Finally join iterates over the list and joins each with a single space in between.

Upvotes: 6

Ashok Kumar
Ashok Kumar

Reputation: 471

Using regular expressions when cleaning the text is the best practice

def removing_leading_whitespaces(text):
     return re.sub(r"^\s+","",text)

Apply the above function

removing_leading_whitespaces("  Example")
"  Example"   -> "Example"

removing_leading_whitespaces("  Example  ")
"  Example  " -> "Example  "

removing_leading_whitespaces("    Example")
"    Example" -> "Example"

Upvotes: 3

Jaymon
Jaymon

Reputation: 5698

The question doesn't address multiline strings, but here is how you would strip leading whitespace from a multiline string using python's standard library textwrap module. If we had a string like:

s = """
    line 1 has 4 leading spaces
    line 2 has 4 leading spaces
    line 3 has 4 leading spaces
"""

if we print(s) we would get output like:

>>> print(s)
    this has 4 leading spaces 1
    this has 4 leading spaces 2
    this has 4 leading spaces 3

and if we used textwrap.dedent:

>>> import textwrap
>>> print(textwrap.dedent(s))
this has 4 leading spaces 1
this has 4 leading spaces 2
this has 4 leading spaces 3

Upvotes: 19

coobird
coobird

Reputation: 160954

The lstrip() method will remove leading whitespaces, newline and tab characters on a string beginning:

>>> '     hello world!'.lstrip()
'hello world!'

Edit

As balpha pointed out in the comments, in order to remove only spaces from the beginning of the string, lstrip(' ') should be used:

>>> '   hello world with 2 spaces and a tab!'.lstrip(' ')
'\thello world with 2 spaces and a tab!'

Related question:

Upvotes: 450

Tenzin
Tenzin

Reputation: 2505

If you want to cut the whitespaces before and behind the word, but keep the middle ones.
You could use:

word = '  Hello World  '
stripped = word.strip()
print(stripped)

Upvotes: 31

Marquis Wang
Marquis Wang

Reputation: 11108

The function strip will remove whitespace from the beginning and end of a string.

my_str = "   text "
my_str = my_str.strip()

will set my_str to "text".

Upvotes: 125

cjs
cjs

Reputation: 27191

To remove everything before a certain character, use a regular expression:

re.sub(r'^[^a]*', '')

to remove everything up to the first 'a'. [^a] can be replaced with any character class you like, such as word characters.

Upvotes: 14

Related Questions