Bytes Methods

bytes.ljust()

Pad data to at least width bytes, aligned left. The fill argument must be a one-byte bytes or bytearray object in CPython 3.13.5; a memoryview is rejected. Width counts bytes, not decoded characters or terminal columns. Widths no larger than the current length do not truncate. Returns bytes, which may reuse the original object.

Formal Syntax & Structure

python syntaxpython
data.ljust(width, fillbyte=b" ", /)

Parameters & Attributes

NameTypeDescription
widthintMinimum total length in bytes.
fillbytebytes | bytearrayExactly one padding byte; default ASCII space.

Return Value

bytes

bytes padded to at least width bytes.

Working Example

Example Snippetpython
print(bytes(b'AB').ljust(6, b'.'))print(bytes(b'ABC').ljust(7))print(bytes(b'ABC').ljust(2))
Python Reference