The rjust Method in Python: Aligning a String to the Right

Aligns a string to the right, padding it on the left to the specified length with a chosen character.

rjust(width[, fillchar])

  • width -- desired minimum length of the resulting string
  • fillchar -- character used to pad the string; defaults to a space

The original string is not shortened, even if it has fewer characters than the specified length.

''.rjust(3, 'w')  # 'www'
'1'.rjust(4, 'w')  # 'www1'
'1'.rjust(0, 'w')  # '1'
'1'.rjust(4)  # '   1'