Python 3 reference · String Methods

Python String ljust() Method

Return the string left-justified in a field of the given width, padded on the right with a fill character.

Python 3 Since 3.0

Syntax

str.ljust(width, fillchar=' ')

Parameters

ParameterDescription
width Total length of the result.
fillchar optional A single padding character. Default: ' '

Return value

A new string.

Examples

Pad on the rightTry it yourself
print("[" + "left".ljust(10) + "]")
print("Chapter 1".ljust(20, ".") + " 3")

Output

[left      ]
Chapter 1........... 3
Align a price listTry it yourself
for item, price in [("Tea", 2.5), ("Cake", 4)]:
    print(item.ljust(8) + f"{price:>6.2f}")

Output

Tea       2.50
Cake      4.00

Version history

VersionChange
Python 3.0Added