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.

Syntax

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

x.ljust(length, character)

where

Parameter Required / Optional Description
length Required An integer representing the length of resulting string.
character Optional A character to fill the missing space.

Examples

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.

Example.py

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

Output

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

Conclusion

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