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
| Parameter | Description |
|---|---|
width |
Total length of the result. |
fillchar optional |
A single padding character. Default: ' ' |
Return value
A new string.
Examples
print("[" + "right".rjust(10) + "]")
print("7".rjust(3, "0"))
Output
[ right] 007
for n in [5, 42, 1234]:
print(str(n).rjust(6))
Output
5
42
1234Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.ljust()· Return the string left-justified in a field of the given width, padded on the right with a fill character.str.center()· Return the string centred in a field of the given width, padded on both sides with a fill character (a space by default).str.zfill()· Return the string padded on the left with zeros to the given width, keeping a leading + or - sign in front.- Python Strings · tutorial