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

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

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)
Try Online

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.