bytes.rjust()
The usual choice for numeric columns. Unlike zfill it knows nothing about signs, so a zero fill on a negative number buries the minus.
Common call
amount.rjust(10)
Returns
a new bytes object at least width long
Replaces
fill * (width - len(data)) + data, which goes negative on wide data
Watch out
rjust(5, b'0') on b'-42' gives b'00-42'; use zfill for numbers
bytes.rjust(width[, fillbyte])
→ bytes
Demo
Live evaluation
Try:
Inputs
sstrdata (encoded as utf-8)
widthinttotal width
fillstrfill byte (one char)
Output
bytes('ab', 'utf-8').rjust(5, bytes('-', 'utf-8'))
b'---ab'
Fill bytes are added on the left until the width is reached, pushing the data to the right edge. The negative case is the one to notice: rjust has no idea that -42 is a number, so the zeros go in front of the minus sign and you get 00-42. That is exactly the case zfill exists for. Data already at or beyond the width comes back unchanged.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| width | int | yes | Minimum total length. Shorter widths return the data unchanged. |
| fillbyte | bytes | no (b' ') | A bytes object of length exactly 1. Anything else raises TypeError. |
Return value
bytes — A new bytes object padded on the left with fillbyte to at least width. Data already that long is returned unchanged.
Common patterns
Right-aligned numeric column
Numbers line up on their last digit.
row = name.ljust(20) + amount.rjust(10)
Fixed-width field with a custom fill
Legacy formats sometimes want a specific pad byte.
field = value.rjust(8, b'\x00')
Zero-pad a number correctly
zfill keeps the sign in front; rjust does not.
num.zfill(8) # not num.rjust(8, b'0')
Examples
1. Pad left
b'ab'.rjust(5, b'-')
Returns
b'---ab'2. Default space
b'ab'.rjust(4)
Returns
b' ab'3. Zero fill
b'42'.rjust(5, b'0')
Returns
b'00042'4. Sign buried
b'-42'.rjust(5, b'0')
Returns
b'00-42'5. zfill instead
b'-42'.zfill(5)
Returns
b'-0042'6. Already wide
b'abcdef'.rjust(3)
Returns
b'abcdef'Pitfalls
1. A zero fill buries the sign
rjust is not numeric-aware. Padding a negative number with zeros puts them before the minus, producing something that is not a valid number at all.
Not a number
b'-42'.rjust(5, b'0')
b'00-42'
Use zfill
b'-42'.zfill(5)
b'-0042'
2. It never truncates
Data longer than the width is returned as-is. A column can silently overflow its allotted space.
Overflows
b'toolong'.rjust(4)
b'toolong'
Slice then pad
b'toolong'[-4:].rjust(4)
b'long'
3. Width counts bytes, not characters
Encoded text with multi-byte characters is wider than it looks, so columns drift.
Off by bytes
'é'.encode().rjust(3, b'-')
b'-\xc3\xa9'
Pad text
'é'.rjust(3, '-').encode()
padded by character
When to use
Use it
- Right-aligned columns in fixed-width byte output
- Padding a field on the left with a non-zero byte
Reach for something else
- Zero-padding numbers → zfill, which respects the sign
- Padding on the RIGHT → ljust
- Text with non-ASCII characters → decode, pad, encode
Notes
Complexity
O(width) — one allocation and fill
Return
A new bytes object; the original when already wide enough
CPython impl
Objects/bytesobject.c :: stringlib_rjust
Memory
Allocates a buffer of max(len, width)
Thread-safe
Yes — bytes are immutable
FAQ
zfill, for anything numeric — it moves the zeros after a leading sign. rjust treats the sign as just another byte and pads in front of it.
b'-42'.rjust(5, b'0') # b'00-42' b'-42'.zfill(5) # b'-0042'
History
3.0
bytes.rjust arrived with the bytes type in the text/binary split.