String method

str.rjust()

Returns a copy of a string padded on the left to the width you choose, so the text lines up on the right side.

BeginnerEvery example below runs in your browser. Change the code and press Run.

str.rjust(width, fillchar=' ', /)

rjust() ("right-justify") adds padding before the text until it is width characters long, so the text ends at the right edge.

That is how numbers are usually lined up in a table: with the last digits under each other. Give a second argument to pad with another character.

Like str.ljust(), it never cuts text that is already long enough, and the fill character must be a single character. To pad numbers with zeros, str.zfill() also handles a minus sign correctly.

Parameters

ParameterWhat it is
width
required
The total length of the result.
fillchar
optional, default ' '
The single character used for padding. Pass it by position, as in rjust(6, "*"): writing fillchar= raises TypeError.

Return value

A new string of length width (or the original, if it is longer).

Examples

1Right-align numbers

Pythoneditable
for amount in ["3.50", "12.00", "150.25"]:
    print(amount.rjust(8))
Output
    3.50
   12.00
  150.25

2A small table

Pythoneditable
rows = [("Rent", 950), ("Food", 320), ("Phone", 25)]
for label, cost in rows:
    print(label.ljust(8) + str(cost).rjust(6))
Output
Rent       950
Food       320
Phone       25

3Pad with another character

Pythoneditable
print("42".rjust(6, "*"))
print("7".rjust(3, "0"))
Output
****42
007

4rjust() vs zfill() with a minus sign

Pythoneditable
print("-5".rjust(4, "0"))
print("-5".zfill(4))
Output
00-5
-005

rjust() just adds characters in front; zfill() keeps the sign first.

Common mistakes

Padding a number instead of a string

Integers have no rjust(). Use str(n).rjust(5) or an f-string: f"{n:>5}".

Pythoneditable
n = 42
try:
    n.rjust(5)
except AttributeError:
    print("AttributeError: int has no rjust()")
print(str(n).rjust(5))
print(f"{n:>5}")
Output
AttributeError: int has no rjust()
   42
   42

Questions people ask

How do I right-align with an f-string?

f"{value:>10}", and f"{price:>10.2f}" for a number with two decimals.

How do I add zeros in front of a number?

Use str.zfill() or f"{n:05d}".

What is the opposite of rjust()?

str.ljust() pads on the right; str.center() pads on both sides.

Spotted a mistake on this page? Tell us