Python string Method - ljust()

The ljust() method is a built-in string method in Python that returns a left-justified version of the original string. The method takes two arguments: width and fillchar. The width parameter specifies the length of the returned string, and the fillchar parameter is an optional argument that specifies the character that should be used to fill any extra space in the resulting string. If no fillchar is provided, the space character ' ' is used by default.

Here is the syntax of the ljust() method:

string.ljust(width[, fillchar])
Source:w‮i.ww‬giftidea.com
  • string: Required. The string that you want to left-justify.
  • width: Required. An integer that represents the width of the resulting string.
  • fillchar: Optional. A character that is used to fill any extra space in the resulting string. Default is ' '.

And here is an example usage of the ljust() method:

original_string = "Hello, World!"
new_string = original_string.ljust(20, '-')
print(new_string)

This would output:

Hello, World!-------

In this example, we left-justified the string "Hello, World!" with a total width of 20 characters and filled any extra space with the '-' character.