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.
print("42".rjust(6, ".")) # ....42
Try it
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.
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
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
widthis returned as is. fillcharmust be one character. A longer string raisesTypeError.- Counts code points, not display columns. Wide characters (CJK, emoji) and combining marks can still misalign output.
- Numbers must be converted with
str()first, becauserjust()is a string method. An f-string such asf"{n:>6}"does both steps. - Zero padding of numbers:
zfill()puts zeros after a sign (-00042), whilerjust(6, "0")puts them before it (000-42). - Siblings:
ljust()pads on the right andcenter()on both sides. - Immutable. The original string is not changed.
- Also on
bytesandbytearray, with a bytes fill character.