Python Tutorial Python Advanced Python Resources Python Libraries
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Python - String ljust() Method



The Python String 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.

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

5