Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท String Methods

Python str.rjust() method

Reference for Python str.rjust(): right-align a string by padding on the left to a given width with spaces or a fill character, plus alternatives like f-strings.

The str.rjust() method returns the string padded on the left so that the result is at least a given width, which pushes the text to the right edge. By default it pads with spaces, but you can supply any single character.

It is handy for aligning numbers in columns. If the string is already as long as the width (or longer), it is returned unchanged; rjust() never truncates.

Python
print("42".rjust(6, "."))   # ....42

Try it

Python Aligning a column

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python rjust, zfill and f-strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Syntax

Python
str.rjust(width)
str.rjust(width, fillchar)
Name Description
width The minimum length of the result.
fillchar Optional. Exactly one character used for padding (default is a space).

Return value: a new string of length max(width, len(s)).

Raises: TypeError if width is not an integer or fillchar is not a single character.

Usage notes

  • Never truncates. A string longer than width is returned as is.
  • fillchar must be one character. A longer string raises TypeError.
  • Counts code points, not display columns. Wide characters (CJK, emoji) and combining marks can still misalign output.
  • Numbers must be converted with str() first, because rjust() is a string method. An f-string such as f"{n:>6}" does both steps.
  • Zero padding of numbers: zfill() puts zeros after a sign (-00042), while rjust(6, "0") puts them before it (000-42).
  • Siblings: ljust() pads on the right and center() on both sides.
  • Immutable. The original string is not changed.
  • Also on bytes and bytearray, with a bytes fill character.