bytes.lstrip()

Half of strip. Same set-not-sequence rule for the argument, applied to the left end only.

Bytes methodPython 3.0+Live demo
Common call
data.lstrip()
Returns
a new bytes object with the left end trimmed
Replaces
scanning for the first byte not in a set
Watch out
lstrip(b'ab') removes any leading a or b bytes, not the sequence ab
bytes.lstrip([chars])
→ bytes

Demo

Live evaluation
Try:
Inputs
sstrdata (encoded as utf-8)
charsstrbyte values to strip
Output
bytes('xxabxx', 'utf-8').lstrip(bytes('x', 'utf-8'))
b'abxx'

Only the left end is touched — xxabxx becomes abxx, with the trailing x bytes kept. The second case shows the set rule: stripping "ab" from "abcba" removes a then b from the front and stops at c, leaving cba. It is a set of bytes to keep removing while they match, not a prefix to match once.

Parameters

NameTypeRequiredDescription
charsbytesno (None)A SET of byte values to strip from the left. Omitted or None strips ASCII whitespace.

Return value

bytes — A new bytes object with leading bytes removed. The right end is untouched.

Common patterns

Remove leading whitespace only
Keeps a trailing newline that a later step may need.
body = line.lstrip()
Skip leading padding bytes
Fixed-width fields sometimes left-pad with nulls or zeros.
value = field.lstrip(b'0')
Remove an exact prefix instead
When you mean a specific sequence, removeprefix is the tool.
data.removeprefix(b'http://')

Examples

1. Left only
b'xxabxx'.lstrip(b'x')
Returns
b'abxx'
2. Set, not sequence
b'abcba'.lstrip(b'ab')
Returns
b'cba'
3. Default whitespace
b' ab '.lstrip()
Returns
b'ab '
4. Nothing matches
b'abc'.lstrip(b'z')
Returns
b'abc'
5. Everything goes
b'aaa'.lstrip(b'a')
Returns
b''
6. Empty
b''.lstrip(b'x')
Returns
b''

Pitfalls

1. chars is a SET, not a prefix
The classic strip mistake. Passing a word removes its letters one at a time from the left until something else appears — not the word itself.
Letters removed
b'http://x'.lstrip(b'http://')
b'x' # the h,t,p,:,/ bytes all stripped
Use removeprefix
b'http://x'.removeprefix(b'http://')
b'x' # same here, but differs on b'https://x'
2. It only touches the left end
Trailing bytes in the set survive. Reaching for lstrip to clean both ends leaves the right side dirty.
Right kept
b'xxabxx'.lstrip(b'x')
b'abxx'
Use strip
b'xxabxx'.strip(b'x')
b'ab'
3. The argument must be bytes
A str raises. The no-argument form is fine, so this often appears only when an explicit set is added.
str rejected
b'xxa'.lstrip('x')
TypeError: a bytes-like object is required, not 'str'
Bytes literal
b'xxa'.lstrip(b'x')
b'a'

When to use

Use it
  • Removing leading whitespace while keeping the end intact
  • Skipping leading padding in a fixed-width field
Reach for something else
  • Removing a known prefix → removeprefix
  • Cleaning both ends → strip
  • Removing bytes from the middle → replace

Notes

Complexity
O(n) worst case, usually a few bytes at the left end
Return
A new bytes object; the original when nothing was trimmed
CPython impl
Objects/bytesobject.c :: bytes_lstrip
Memory
Allocates the trimmed result
Thread-safe
Yes — bytes are immutable

FAQ

Because it strips the SET of bytes h, t, p, colon and slash from the left, repeatedly. On b"http://x" that happens to give b"x". On b"https://x" it strips h, t, t, p and then stops at the s — which is not in the set — leaving the mangled b"s://x". removeprefix matches the exact sequence and is what you want.

b'https://x'.lstrip(b'http://')        # b's://x' — mangled
b'https://x'.removeprefix(b'http://')  # b'https://x' — unchanged, correct

History

3.0
bytes.lstrip arrived with the bytes type in the text/binary split.