str.ljust()

Left-align a string in a fixed-width field. Padding goes on the right — a copy is returned; the original is unchanged. Never truncates.

String methodPython 1.0+Live demo
Common call
"name".ljust(10)
Returns
new str, length ≥ width
Replaces
sprintf("%-*s") style formatting
Watch out
width ≤ len returns the original untouched; fillchar must be exactly one character
str.ljust(widthwidthTarget minimum length. Shorter strings are padded on the right; longer or equal ones are returned unchanged.type: int · required, fillcharfillcharSingle-character string used for right-side padding. Multi-char or empty raises TypeError.type: str · default: " "=" ")
str

Demo

Live evaluation
Try:
Inputs
stringstrthe source
widthinttarget width
fillcharstrempty = space
Output
'name'.ljust(10)
'name '

ljust pads the original on the RIGHT until the total length equals `width`. If the string is already at or past `width`, the original is returned unchanged — ljust never truncates. fillchar must be exactly one character; empty falls back to the default space.

Parameters

NameTypeRequiredDescription
widthintyesTarget minimum length. Shorter strings are padded on the right; longer or equal ones are returned unchanged.
fillcharstrno (" ")Single-character string used for right-side padding. Multi-char or empty raises TypeError.

Return value

strA copy of the string left-aligned in a field of at least `width` characters, padded on the RIGHT with `fillchar`.

Common patterns

Fixed-width labels
Left-align a column of names or IDs.
for name, count in rows:
    print(name.ljust(20) + str(count))
Dotted table of contents
Fill characters make classic dotted layouts.
line = title.ljust(60, ".") + str(page)
CLI two-column output
ljust on the left, rjust on the right, no manual counting.
print(name.ljust(20) + str(count).rjust(6))

Examples

1. Basic
"name".ljust(10)
Returns
"name "
2. With fill char
"chapter".ljust(15, ".")
Returns
"chapter........"
3. No padding needed
"exact".ljust(5)
Returns
"exact"
4. Never truncates
"long text".ljust(3)
Returns
"long text" # returned unchanged
5. Empty string padding
"".ljust(5, "-")
Returns
"-----"

Pitfalls

1. Never truncates
width ≤ len returns the original unchanged, not a shortened version. Great as a "pad only if needed" primitive; wrong when you actually want a fixed length.
Overflow
"long text".ljust(3)
"long text" # 9 chars, not 3
Slice first
s[:width].ljust(width)
true fixed width
2. fillchar must be exactly one character
Empty or multi-character fillchar raises TypeError. Emoji that are single codepoints work; grapheme clusters (emoji + modifier) do not.
Multi-char fill
"hi".ljust(10, "-=")
TypeError: The fill character must be exactly one character long
One character
"hi".ljust(10, "-")
"hi--------"
3. Original string is NOT modified
Like all string methods, ljust returns a new string. Assigning it back is required for the padded value to persist.
Lost result
s = "hi"
s.ljust(10)
print(s)
"hi" # unchanged
Capture it
s = s.ljust(10)
print(s)
"hi "
4. Padding for numbers reads better as rjust
Left-aligning numeric strings puts the ones column at inconsistent horizontal positions — hard to eyeball columns of totals.
Ragged right
for n in [3, 42, 500]:
    print(str(n).ljust(6))
"3 " "42 " "500 " # decimals misaligned
Use rjust
for n in [3, 42, 500]:
    print(str(n).rjust(6))
" 3" " 42" " 500" # aligned on the right

When to use

Use it
  • Left-aligned labels in text UIs and CLI output
  • Dotted / dashed table-of-contents layouts
  • Fixed-width name or ID columns
  • "Pad only if needed" workflows where truncation would be wrong
Reach for something else
  • Numeric columns → rjust reads better for right-aligned numbers
  • Centered content → str.center
  • You also need to truncate → slice first, then ljust
  • Padding on the left → rjust

Notes

Complexity
O(width)
Return
A new string; the original is unchanged (strings are immutable)
CPython impl
Objects/unicodeobject.c :: pad — with right-side padding only
Memory
Allocates one new string of length max(len, width)
Thread-safe
Yes — strings are immutable

FAQ

The `<` alignment in format() and f-strings does the same thing. `f"{s:<10}"` left-aligns in a field of 10, with space fill by default. Use format when you compose with other spec fields (fill, sign, precision).

History

1.0
ljust() has been part of str since Python 1.0.
2.4
fillchar parameter added.