Home / Python / Reference / String methods

Python string ljust() method

Pad a string on the right so it is left-aligned in a fixed width.

Syntax

str.ljust(width, fillchar=" ")

Good for neat text tables.

Parameters

widthtotal width
fillcharthe padding character

Return value

str

Example

Code

for item, price in [("coffee", 4.5), ("muffin", 5.25)]:
    print(item.ljust(10, "."), f"{price:5.2f}")

Output

coffee....  4.50
muffin....  5.25

Related

Learn it step by step in the free lesson Strings, with exercises that check your answers.