Python bytes.rjust() Method

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

Definition

bytes.rjust(width, fillbyte) returns a copy of the data with fill bytes added on the left until it is width bytes long. The default fill is an ASCII space.

Right alignment suits numbers: amounts, sizes, and offsets line up on their last digit. With b"0" as the fill it zero-pads, but unlike zfill() it does not treat a sign specially.

bytes.rjust() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.rjust(...). 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.rjust(width)
result = data.rjust(width, fillbyte)
CallWhat it does
b"42".rjust(5)Returns b' 42'.
b"ff".rjust(4, b"0")Returns b'00ff'.
b"12345".rjust(3)Returns b'12345'.

Data already at least width bytes long is returned unchanged.

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

Parameters

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

Return Value

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

Python Examples

Example 1 — Right-Align Amounts in a Receipt

Convert each amount to ASCII bytes and right-align it in six bytes so the digits line up.

python
for amount in [5, 120, 98765]:
    print(str(amount).encode("ascii").rjust(6) + b" EUR")

How It Works

  1. Number to bytes. str(amount).encode("ascii") produces the digits as bytes.
  2. Last digits aligned. Each value is padded on the left to six bytes, so units line up.
  3. Add a suffix. Concatenating b" EUR" keeps the result as bytes.

Example 2 — Zero Padding with rjust() and zfill()

Both can add leading zeros, but they treat a minus sign differently.

python
print(b"-7".rjust(4, b"0"))
print(b"-7".zfill(4))
print(b"ff".rjust(4, b"0"))

How It Works

  1. Sign buried. rjust() just prepends zeros, so the minus ends up in the middle.
  2. Sign kept first. zfill() puts the zeros after the sign, which is what numbers need.
  3. Hex digits. For unsigned hex text, rjust(4, b"0") works well.

Example 3 — Right-Align Offsets in a Mini Dump

Print a short message in 5-byte rows with each row's starting offset right-aligned in three columns.

python
data = b"Hello, bytes!"

for offset in range(0, len(data), 5):
    label = str(offset).encode("ascii").rjust(3, b" ")
    print(label, data[offset:offset + 5])

How It Works

  1. Rows of five. range(0, 13, 5) gives the offsets 0, 5, and 10.
  2. Aligned labels. One- and two-digit offsets are padded to three bytes so the data column starts in the same place.
  3. Short last row. Slicing past the end is safe; the final row simply holds the three remaining bytes.

Frequently Asked Questions

It pads the data on the left with a fill byte (a space by default) until it reaches the given width.
zfill() keeps a leading + or - before the zeros; rjust() puts the zeros in front of everything.
No. Data longer than width is returned unchanged.
Yes: data.rjust(width, b"\x00").
Yes, and it returns a new bytearray.

Next: bytes.rpartition()

Split at the last separator into a 3-tuple: before, separator, after.

rpartition() →

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.

9 people found this page helpful