Python String ljust()

Python String ljust() is used to left align the given string in specified space (length) and optionally fill the left out space with the specified character.

In this tutorial, we will learn the syntax and examples for ljust() method of String class.


The syntax to call ljust() method on string x in Python is

x.ljust(length, character)


ParameterRequired / OptionalDescription
lengthRequiredAn integer representing the length of resulting string.
characterOptionalA character to fill the missing space.


In the following program, we take a string, and left align the string in a space of 20, and fill the left out space with '-' character.

x = 'hello world'
result = x.ljust(20, '-')
print('Original String : ', x)
print('Result String   : ', result)
Try Online


Original String :  hello world
Result String   :  hello world---------


In this Python Tutorial, we learned how to left align a string using ljust() method, with examples.