Python String - ljust() Method
The Python ljust() method returns the string with whitespaces added at the end of the string until its length reaches the specified length. This method has a optional parameter which can be used to specify a character to fill the missing space (default is whitespace).
Syntax
string.ljust(length, character)
Parameters
length |
Required. specify length of string after filling with specified character. |
character |
Optional. specify character to fill the missing space. default value is whitespace. |
Return Value
Returns the string with whitespaces added at the end of the given string until its length reaches the specified length.
Example:
In the example below, ljust() method returns the string with the specified character added at the end of the string until its length reaches the specified length.
MyString = "Learning Python" print(MyString.ljust(20), "is fun.") MyString = "Learn Python" print(MyString.ljust(20, "#"))
The output of the above code will be:
Learning Python is fun. Learn Python########
❮ Python String Methods