Trim from the LEFT — whitespace by default, or any character in the given set. Not a prefix stripper.
String methodPython 1.0+Live demo
Common call
line.lstrip()
Returns
new str — the original is unchanged
Replaces
a manual `while s and s[0] in chars: s = s[1:]` loop
Watch out
chars is a SET of characters, not a substring — see pitfalls
str.lstrip(charschars — A string of characters to strip. Every character in this string is treated as an individual character to remove, in any order. None (default) strips Unicode whitespace.type: str | None · default: None=None)
→ str
Demo
Live evaluation
Try:
Inputs
stringstrthe source
charsstrempty = whitespace
Output
' hello world'.lstrip()
'hello world'
lstrip removes leading characters. With no argument, it strips Unicode whitespace. With a string argument, it strips ANY character in that set — order does not matter, "xy" and "yx" behave identically. The right side is left alone. If the first character does not match, nothing is stripped and the original is returned unchanged.
Parameters
Name
Type
Required
Description
chars
str | None
no (None)
A string of characters to strip. Every character in this string is treated as an individual character to remove, in any order. None (default) strips Unicode whitespace.
Return value
str — A copy of the string with LEADING characters removed. Default strips whitespace; with an argument, strips any characters in the given SET (not a substring).
Common patterns
Strip leading whitespace
The default no-arg form — matches every Unicode whitespace character.
text = raw_line.lstrip()
Strip leading zeros
A common numeric-normalization step.
digits = "00042".lstrip("0") # "42"
Chain both sides
When you want left trimming only sometimes.
ifline.startswith("#"):
line = line.lstrip("#").rstrip()
Examples
1. Whitespace default
" hello".lstrip()
Returns
"hello"
2. Tabs and newlines
"\t\n hi".lstrip()
Returns
"hi"
3. Leading zeros
"00042".lstrip("0")
Returns
"42"
4. Char set (order agnostic)
"xxyyaabc".lstrip("xy")
Returns
"aabc"
5. Nothing to strip
"hello".lstrip("xy")
Returns
"hello"
6. Right side untouched
" x ".lstrip()
Returns
"x "
Pitfalls
1. chars is a SET of characters, NOT a prefix
The single most common lstrip bug. Passing "https://" strips any leading "h", "t", "p", "s", ":", or "/" — in any order and any quantity — until it hits something else. It does NOT strip the string "https://" specifically.
Ate too much
"https://python.org".lstrip("https://")
"python.org" # or worse — chars matched greedily
Use removeprefix
"https://python.org".removeprefix("https://")
"python.org" # exact prefix, no character-set surprise
2. chars order does not matter
The argument is treated as a set. "abc", "cba", and "aabbc" all behave identically. Trying to encode a specific sequence via order fails.
Order-sensitive attempt
"aabbccxx".lstrip("cba")
"xx" # same as .lstrip("abc")
Do not encode order in chars
# to strip a specific sequence, use removeprefix or slicing
3. Original string is NOT modified
Like all string methods, lstrip returns a new string. Assigning it back is required for the stripped value to persist.
Lost result
s = " hi"s.lstrip()
print(s)
" hi" # unchanged
Capture it
s = s.lstrip()
print(s)
"hi"
When to use
Use it
Trimming leading whitespace (the default no-arg form)
Stripping leading zeros, hashes, dashes, or any character SET
Cleaning up left-side padding characters
When both left- and right-side stripping would be too much
Reach for something else
Removing a SPECIFIC prefix string → str.removeprefix (3.9+)
Both sides → str.strip
Right side only → str.rstrip
Regex-based cleanup → re.sub
Notes
Complexity
O(n) worst case — scans until the first non-matching character
Return
A new string; the original is unchanged (strings are immutable)
Because the argument is a character SET, not a substring. Every character in "https://" — h, t, p, s, colon, slash — is treated as a candidate to strip. It removes any leading occurrence of any of those characters. Since Python 3.9, use str.removeprefix() when you want exact-prefix removal.
History
1.0
lstrip() has been part of str since Python 1.0.
2.2.1
chars parameter added.
3.9
removeprefix() introduced as the intent-preserving alternative for exact-prefix removal.