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
| Parameter | Description |
|---|---|
width |
Total length of the result. |
fillchar optional |
A single padding character. Default: ' ' |
Return value
A new string.
Examples
print("[" + "left".ljust(10) + "]")
print("Chapter 1".ljust(20, ".") + " 3")
Output
[left ] Chapter 1........... 3
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
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.rjust()· Return the string right-justified in a field of the given width, padded on the left with a fill character.str.center()· Return the string centred in a field of the given width, padded on both sides with a fill character (a space by default).str.zfill()· Return the string padded on the left with zeros to the given width, keeping a leading + or - sign in front.- Python Strings · tutorial