Python String ljust() Method

Example

Returns the left-aligned version of the word "banana" with a length of 20 characters:

txt = "banana"
x = txt.ljust(20)
print(x, "is my favorite fruit.")

Run Instance

Note: As a result, the right side of the word "banana" actually has 14 spaces.

Definition and Usage

The ljust() method uses the specified character (space by default) as the padding character to align the string to the left.

Syntax

string.ljust(length, character)

Parameter Value

Parameter Description
length Required. The length of the returned string.
character Optional. Used to fill missing spaces (on the right side of the string) with characters. The default value is " " (space).

More Examples

Example

Use the letter "O" as the padding character:

txt = "banana"
x = txt.ljust(20, "O")
print(x)

Run Instance