Bytearray Methods
bytearray.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 a new bytearray even without padding.
Formal Syntax & Structure
python syntaxpython
data.ljust(width, fillbyte=b" ", /)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| width | int | Minimum total length in bytes. |
| fillbyte | bytes | bytearray | Exactly one padding byte; default ASCII space. |
Return Value
bytearray
bytearray padded to at least width bytes.
Working Example
Example Snippetpython
print(bytearray(b'AB').ljust(6, b'.'))print(bytearray(b'ABC').ljust(7))print(bytearray(b'ABC').ljust(2))