The ljust()
method in Python is a string method that returns a left-justified version of the original string. It pads the original string with spaces on the right-hand side until it reaches the specified length. This method takes one argument which specifies the total length of the resulting string after justification.
Parameter Values
Parameter | Description |
---|---|
width | The width of the resulting string after padding. |
fillchar | Optional. A character that will be used to fill in the padding. Default is whitespace. |
Return Values
The ljust()
method from Python string methods returns a str
object.
How to Use ljust()
in Python
The ljust()
method returns a left-justified string of a specified length by padding the original string with spaces on the right.
print('hello'.ljust(10))
If a fill character is provided, the original string is padded with that character instead of spaces.
print('world'.ljust(8, '*'))
If the specified length is less than the length of the original string, the method returns the original string.
print('python'.ljust(4))