str.rjust
Right-align the string, padding it on the left to the length width.
Python 3.13
str.rjust(width[, fillchar])Parameters
- widthint
- The length of the result.
- fillcharstroptional
- The padding character, a space by default; exactly one character.
Return value
A new string; the original is not changed.
Examples
Padding with spaces on the left
Python 3.13
print('[' + '42'.rjust(5) + ']')[ 42]
Numbers right-aligned in a column
Python 3.13
for number in [5, 42, 300]:
print(str(number).rjust(4))5 42 300
Details
It is handy for columns of numbers. For leading zeros, there is str.zfill, which handles the minus sign correctly. In f-strings, the {text:>10} format does the same.
