str.rjust()
Right-align a string in a fixed-width field. Padding goes on the left — a copy is returned; the original is unchanged. Never truncates.
Common call
str(n).rjust(6)
Returns
new str, length ≥ width
Replaces
sprintf("%*s") style right-alignment
Watch out
width ≤ len returns the original untouched; fillchar must be exactly one character
str.rjust(widthwidth — Target minimum length. Shorter strings are padded on the left; longer or equal ones are returned unchanged.type: int · required, fillcharfillchar — Single-character string used for left-side padding. Multi-char or empty raises TypeError.type: str · default: " "=" ")
→ str
Demo
Live evaluation
Try:
Inputs
stringstrthe source
widthinttarget width
fillcharstrempty = space
Output
'42'.rjust(6)
' 42'
rjust pads the original on the LEFT until the total length equals `width`. If the string is already at or past `width`, the original is returned unchanged — rjust never truncates. fillchar must be exactly one character; empty falls back to the default space.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| width | int | yes | Target minimum length. Shorter strings are padded on the left; longer or equal ones are returned unchanged. |
| fillchar | str | no (" ") | Single-character string used for left-side padding. Multi-char or empty raises TypeError. |
Return value
str — A copy of the string right-aligned in a field of at least `width` characters, padded on the LEFT with `fillchar`.
Common patterns
Right-aligned numeric column
Numbers align cleanly on the ones column when right-justified.
for n in totals: print(str(n).rjust(8))
Line numbers in printouts
Fixed-width line numbers keep code listings visually aligned.
for i, line in enumerate(lines, 1): print(f"{str(i).rjust(4)} {line}")
Two-column CLI output
ljust on the label, rjust on the count — no manual counting.
print(name.ljust(20) + str(count).rjust(6))
Examples
1. Basic
"42".rjust(6)
Returns
" 42"2. Zero-pad
"42".rjust(6, "0")
Returns
"000042"3. With dot fill
"$5.00".rjust(12, ".")
Returns
".......$5.00"4. No padding needed
"exact".rjust(5)
Returns
"exact"5. Never truncates
"long text".rjust(3)
Returns
"long text" # returned unchanged6. Empty string padding
"".rjust(5, "-")
Returns
"-----"Pitfalls
1. Never truncates
width ≤ len returns the original unchanged, not a shortened version. Great as a "pad only if needed" primitive; wrong when you actually want a fixed length.
Overflow
"long text".rjust(3)
"long text" # 9 chars, not 3
Slice first
s[:width].rjust(width)
true fixed width
2. rjust with "0" is NOT sign-aware — use zfill
Zero-padding negative numbers with rjust puts the zeros before the sign — probably not what you want.
Sign hidden
"-42".rjust(6, "0")
"000-42" # sign lost in the middle
zfill is sign-aware
"-42".zfill(6)
"-00042" # sign stays in front
3. fillchar must be exactly one character
Empty or multi-character fillchar raises TypeError. Emoji that are single codepoints work; grapheme clusters (emoji + modifier) do not.
Multi-char fill
"hi".rjust(10, "-=")
TypeError: The fill character must be exactly one character long
One character
"hi".rjust(10, "-")
"--------hi"
4. Original string is NOT modified
Like all string methods, rjust returns a new string. Assigning it back is required for the padded value to persist.
Lost result
s = "hi" s.rjust(10) print(s)
"hi" # unchanged
Capture it
s = s.rjust(10) print(s)
" hi"
When to use
Use it
- Right-aligned numeric columns in text output
- Line numbers and index labels in printouts
- Money and quantity columns where the ones digit should align
- "Pad only if needed" workflows where truncation would be wrong
Reach for something else
- Zero-padding numbers with a possible sign → zfill (sign-aware)
- Left-aligned text columns → ljust
- Centered content → str.center
- You also need to truncate → slice first, then rjust
Notes
Complexity
O(width)
Return
A new string; the original is unchanged (strings are immutable)
CPython impl
Objects/unicodeobject.c :: pad — with left-side padding only
Memory
Allocates one new string of length max(len, width)
Thread-safe
Yes — strings are immutable
FAQ
The `>` alignment in format() and f-strings does the same thing. `f"{s:>10}"` right-aligns in a field of 10, with space fill by default. Use format when you compose with other spec fields (fill, sign, precision).
History
1.0
rjust() has been part of str since Python 1.0.
2.4
fillchar parameter added.