Python Function Reference

str.ljust

Left-align the string, padding it on the right to the length width.

Python 3.13
str.ljust(width[, fillchar])

Parameters

widthint
The length of the result.
fillcharstroptional
The padding character, a space by default; exactly one character.

Return value

A new string; the original is not changed.

Examples

Padding with spaces on the right

Python 3.13
print('[' + 'hi'.ljust(5) + ']')
[hi   ]

A column with dots before the price

Python 3.13
for name, price in [('tea', 50), ('coffee', 120)]:
    print(name.ljust(8, '.') + str(price))
tea.....50
coffee..120

Details

A long string is not cut: if it is already at least width long, it is returned unchanged. In f-strings, the {text:<10} format does the same.

See also