The ljust method

The ljust method returns a string in which the text is filled and aligned to the left. In the first parameter of the method, we pass a number to indicate the length of the string, in the second optional parameter - a filler character, by default it is a space.

Syntax

pythonstring.ljust(stringlength, [placeholder symbol])

Example

Let's apply the ljust method to our string, and specify an exclamation mark as the placeholder:

pythontxt = 'abc'
print(txt.ljust(6, '!'))

Result of code execution:

python'abc!!!'

Example

Let's apply the ljust method to our string without specifying a placeholder:

pythontxt = 'abc'
print(txt.ljust(6))

Result of code execution:

python'abc   '

See also

  • method rjust,
    which aligns and fills the text in a line to the right
  • method zfill,
    which fills the beginning of the string with zeros
  • method format,
    which substitutes data using string formatting