Python String lstrip() Method

Short answer
text.lstrip(chars=None) returns a new string with leading (left-hand) whitespace, or any characters in chars, removed; the right end is left alone.

Use lstrip() when only the beginning of a string is messy, for example indented lines, numbers padded with leading zeros, or text that starts with a bullet symbol. For both ends at once use strip(); for the right end only, rstrip().

Like every string method, it returns a new string and leaves the original unchanged.

Quick facts
Syntaxtext.lstrip(chars=None)
Parameterschars (optional): a string whose characters are all removed from the start; default is whitespace
ReturnsA new str
Time complexityO(n) in the worst case

How lstrip() removes leading characters

Python looks at the first character; if it is whitespace (or in chars) it is dropped, and the check repeats until a character that is not removable is found. Whitespace later in the text is never touched.

Python
line = "   indented text   "
print(repr(line.lstrip()))

lstrip() with chars treats the argument as a character set

Just like strip(), the argument is not a prefix. Every character that appears in chars is removed from the left until something else appears. That makes it useful for zeros or symbols but wrong for removing a word.

Python
print("007".lstrip("0"))
print("0000".lstrip("0") == "")
print("abcabc-text".lstrip("abc"))

To drop an exact prefix once, use removeprefix() (Python 3.9+).

lstrip() vs removeprefix() vs slicing

GoalCodeResult for the given input
Remove leading spaces" hi".lstrip()'hi'
Remove leading zeros"0042".lstrip("0")'42'
Remove an exact prefix"item_5".removeprefix("item_")'5'
Remove the first N characters"item_5"[5:]'5'
Misuse: prefix via lstrip"item_5".lstrip("item_")'5' only by luck; fails for "item_t"

Examples you can run

Press Run, then edit the code and run it again.

Trim leading spaces from an indented line

Only the left side changes.

Trim leading spaces from an indented line
line = "    total = 5   "
print(f"[{line.lstrip()}]")
Expected output[total = 5   ]

Remove leading zeros from an ID

The result is still a string, so convert it if you need a number.

Remove leading zeros from an ID
code = "000451"
print(code.lstrip("0"))
print(int(code))
Expected output451
451

Why chars is not a prefix

Each character is removed on its own, so more than the word can disappear.

Why chars is not a prefix
print("ticket-42".lstrip("tick"))
print("ticket-42".removeprefix("tick"))
Expected outputet-42
et-42

Common mistakes

Using lstrip() to remove a prefix word

✗ Wrong

print("www.site.com".lstrip("www."))
site.com works here, but "w.web.com".lstrip("www.") returns "eb.com"

✓ Right

print("w.web.com".removeprefix("www."))

Use removeprefix("www.") for an exact prefix.

Forgetting to keep the result

✗ Wrong

s = "  hi"
s.lstrip()
print(repr(s))
' hi' (unchanged)

✓ Right

s = "  hi"
s = s.lstrip()
print(repr(s))

Assign the new string: s = s.lstrip().

Stripping all zeros from zero

✗ Wrong

print("0".lstrip("0"))
(empty string, not "0")

✓ Right

print(str(int("000")))

Handle the all-zero case, or convert with int() and back.

Python String lstrip() Method: questions people ask

What is the difference between strip() and lstrip()?

strip() cleans both ends of the string, while lstrip() cleans only the left (beginning) side.

How do I remove leading zeros from a string in Python?

Use text.lstrip("0"). If the string might be all zeros, remember it then returns an empty string, so str(int(text)) can be safer.

Does lstrip() remove a prefix?

Not as a unit. It removes any leading characters found in its argument; use removeprefix() to remove an exact prefix.

Keep learning

Step-by-step lessons:
Python StringsPython String Methods

Related:
Python String strip() MethodPython String rstrip() MethodHow to Remove Whitespace from a String in PythonPython String zfill() Method

More in string methods:
Python String split() MethodPython String join() MethodPython String strip() MethodPython String rstrip() MethodPython String replace() MethodPython String upper() MethodPython String lower() MethodPython String title() Method See all →