Python bytes.lstrip() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

bytes.lstrip() returns a new bytes object with bytes removed from the left end only. With no argument it removes ASCII whitespace; with chars it removes any byte found in that set.

Stripping stops at the first byte that is not in the set. Common uses are removing leading zero digits from fixed-width numeric fields and measuring how far a line is indented.

bytes.lstrip() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.lstrip(...). No import is needed, and bytearray objects have the same method.

Syntax

Strip leading whitespace, or a set of bytes you choose.

python
result = data.lstrip()
result = data.lstrip(chars)
CallWhat it does
b" hi ".lstrip()Returns b'hi ': the right side is kept.
b"000120".lstrip(b"0")Returns b'120'.
b"\xff\xfeA".lstrip(b"\xfe\xff")Returns b'A'.

Trailing bytes are never removed by lstrip().

Method signatures
python
bytes.lstrip(chars=None, /)
# chars: bytes-like set of byte values; None means ASCII whitespace

Parameters

ParameterDescription
charsOptional. The set of byte values to remove from the left. Default None removes ASCII whitespace.

Return Value

Return ValueDescription
bytesA copy without the leading bytes that belong to the set.

Python Examples

Example 1 — Drop Leading Zeros from Numeric Fields

Fixed-width records pad numbers with zero digits. Strip them, and fall back to b"0" when nothing is left.

python
fields = [b"000120", b"0000", b"007"]

for f in fields:
    digits = f.lstrip(b"0")
    print(digits, int(digits or b"0"))

How It Works

  1. Zeros removed. b"000120" loses its three leading zeros; the zero at the end stays.
  2. All zeros. An all-zero field becomes empty bytes, which are falsy, so or b"0" supplies a value.
  3. bytes to int. int() accepts ASCII digit bytes directly.

Example 2 — Measure Indentation in Config Lines

Compare the length before and after lstrip() to find how many whitespace bytes start each line.

python
config = b"name=app\n    port=80\n\tdebug=1"

for line in config.split(b"\n"):
    body = line.lstrip()
    print(len(line) - len(body), body)

How It Works

  1. Split first. split() on b"\n" gives one bytes object per line.
  2. Length difference. The number of removed bytes is the indentation: 4 spaces on the second line.
  3. Tabs count as one. The tab is a single whitespace byte, so the third line reports 1.

Example 3 — lstrip() Is Not a Prefix Remover

Stripping www. from a host name removes too much. A startswith check with slicing removes the exact prefix, and a byte-order mark can be stripped as a set.

python
host = b"www.web.org"

print(host.lstrip(b"w."))
if host.startswith(b"www."):
    print(host[len(b"www."):])
print(b"\xff\xfe\xffdata".lstrip(b"\xff\xfe"))

How It Works

  1. Over-stripping. The set {w, .} also matches the w of web, so the result is b'eb.org'.
  2. Exact prefix. Checking with startswith() and slicing off four bytes keeps the real host name.
  3. Where a set helps. Here every leading \xff and \xfe byte is unwanted, so a set is exactly right.

Frequently Asked Questions

lstrip() only removes bytes from the left end; strip() removes them from both ends.
Use data.lstrip(b"0"). Handle the all-zero case, which becomes b"".
The argument is a set of bytes, so any leading w or . is removed. Use removeprefix(b"www.") on Python 3.9+.
No. Pass bytes, such as b" \t".
Space, \t, \n, \r, \x0b, and \x0c, the same set as strip() and split().

Next: bytes.maketrans()

Build a 256-byte translation table for translate().

maketrans() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful