formatting

str.rjust

Returns the string right-justified in a field of the given width, padded on the left with fillchar. Returns the original string unchanged if it is already at least width characters long.

str.rjust(width[, fillchar])

Parameters

Parameter Purpose
width minimum total width
fillchar single character used to pad (default ' ')

Examples

>>> '42'.rjust(6)
'    42'

padded with spaces on the left

>>> '42'.rjust(6, '0')
'000042'

leading zeros — but see zfill for a signed-number-aware variant

>>> 'hello'.rjust(3)
'hello'

no truncation when already wide enough

>>> '$'.rjust(5, '.')
'....$'

any single fill character

Gotcha

Does not truncate. For signed numbers, use zfill() which keeps the sign in front of the padding.

Related methods

← All Python string methods · Python built-ins