Python String ljust() Method

Returns left justified string

Usage

The ljust() method returns left-justified string of length width.

Padding is done using the specified fillchar (default is an ASCII space).

The original string is returned as it is, if width is less than or equal to string length.

Syntax

string.ljust(width,fillchar)

Python String ljust() method parameters
ParameterConditionDescription
widthRequiredThe length of the string
fillcharOptionalA character you want to use as a fill character.
Default is an ASCII space.

Basic Example

Example: Align text left

S = 'Left'
x = S.ljust(12)
print(x)

Output:

Left        

Specify a Fill Character

By default the string is padded with whitespace (ASCII space).

You can modify that by specifying a fill character.

Example: ljust() with ‘*’ as a fill character

S = 'Left'
x = S.ljust(12, '*')
print(x)

Output:

Left********

Equivalent Method

You can achieve the same result by using format() method.

Example: Align text left with format()

S = 'Left'
x = '{:<12}'.format(S)
print(x)

Output:

Left