Python 3 reference · String Methods

Python String rjust() Method

Return the string right-justified in a field of the given width, padded on the left with a fill character.

Python 3 Since 3.0

Syntax

str.rjust(width, fillchar=' ')

Parameters

ParameterDescription
width Total length of the result.
fillchar optional A single padding character. Default: ' '

Return value

A new string.

Examples

Pad on the leftTry it yourself
print("[" + "right".rjust(10) + "]")
print("7".rjust(3, "0"))

Output

[     right]
007
Right-align numbersTry it yourself
for n in [5, 42, 1234]:
    print(str(n).rjust(6))

Output

     5
    42
  1234

Version history

VersionChange
Python 3.0Added