Python bytes.ljust() Method

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

Definition

bytes.ljust(width, fillbyte) returns a copy of the data followed by enough fill bytes to make it width bytes long. The default fill is an ASCII space.

It is ideal for fixed-width records: binary formats often store names in a fixed-size field padded with zero bytes, and text reports use it to line up columns. Data that is already long enough is returned unchanged.

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

Syntax

Give the field width and, optionally, a one-byte fill.

python
result = data.ljust(width)
result = data.ljust(width, fillbyte)
CallWhat it does
b"id".ljust(5)Returns b'id '.
b"ada".ljust(6, b"\x00")Returns b'ada\x00\x00\x00'.
b"toolong".ljust(3)Returns b'toolong'.

To make a field exactly n bytes even when the data is longer, slice first: data[:n].ljust(n, b"\x00").

Method signatures
python
bytes.ljust(width, fillbyte=b' ', /)
# returns a new bytes object (bytearray.ljust() returns a bytearray)

Parameters

ParameterDescription
widthThe minimum length of the result in bytes.
fillbyteOptional. A single byte used for padding on the right. Default b" ".

Return Value

Return ValueDescription
bytesThe data padded on the right to width bytes.

Python Examples

Example 1 — Pack Names into Fixed 8-Byte Records

Each name gets an 8-byte slot padded with zero bytes, so any record can be found by its offset.

python
names = [b"ada", b"grace", b"linus"]

record = b"".join(n.ljust(8, b"\x00") for n in names)
print(len(record))
print(record[:8])
print(record[8:16].rstrip(b"\x00"))

How It Works

  1. Equal slots. Three names times 8 bytes gives a 24-byte record.
  2. Visible padding. The first slot shows ada followed by five zero bytes.
  3. Read back. Slicing the second slot and calling rstrip() recovers the name.

Example 2 — Print a Dotted Status Table

Pad interface names with dots so the states line up, then decode each line for display.

python
items = [(b"eth0", b"up"), (b"wlan0", b"down"), (b"lo", b"up")]

for name, state in items:
    print((name.ljust(8, b".") + state).decode("ascii"))

How It Works

  1. Same start column. Every state begins at column 8 because each name is padded to 8 bytes.
  2. Concatenate bytes. + joins the padded name and the state into one bytes object.
  3. Print text. decode() removes the b'...' wrapper from the output.

Example 3 — Long Data and bytearray Results

See that ljust() leaves long data alone, mark the padding with a bar, and call it on a bytearray.

python
print(b"toolongname".ljust(4))
print(b"id".ljust(5) + b"|")
print(bytearray(b"x").ljust(3, b"-"))

How It Works

  1. No cutting. The 11-byte value is longer than 4, so it is returned as it is.
  2. See the spaces. Adding a bar after the result shows the three padding spaces.
  3. bytearray in, bytearray out. The mutable type returns a padded bytearray.

Frequently Asked Questions

It pads the data on the right with a fill byte (a space by default) until it reaches the given width.
Use data.ljust(width, b"\x00").
No. Slice first, data[:width], if the field must be an exact size.
No. Use a one-byte bytes object such as b" ".
rjust() pads on the left so the data is right-aligned.

Next: bytes.lower()

Return a copy with all ASCII uppercase letters converted to lowercase.

lower() →

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.

8 people found this page helpful