The ljust Method in Python: Left-aligning a String

Left-aligns a string, padding it on the right to the specified length with a chosen character.

ljust(width[, fillchar])

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

The original string is not truncated, even if it is shorter than the specified length.

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

The opposite of this method, which right-aligns the string, is rjust(). To pad the string on both sides, use center().