bytes.lstrip()
Half of strip. Same set-not-sequence rule for the argument, applied to the left end only.
Demo
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
| Name | Type | Required | Description |
|---|---|---|---|
| chars | bytes | no (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
body = line.lstrip()
value = field.lstrip(b'0')
data.removeprefix(b'http://')
Examples
Pitfalls
b'http://x'.lstrip(b'http://')
b'http://x'.removeprefix(b'http://')
b'xxabxx'.lstrip(b'x')
b'xxabxx'.strip(b'x')
b'xxa'.lstrip('x')
b'xxa'.lstrip(b'x')
When to use
- Removing leading whitespace while keeping the end intact
- Skipping leading padding in a fixed-width field
- Removing a known prefix → removeprefix
- Cleaning both ends → strip
- Removing bytes from the middle → replace
Notes
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