str.ljust()

str.ljust(width[, fillchar])
Returns: str · Updated March 13, 2026 · String Methods
strings alignment padding

The .ljust() method returns a left-justified version of the string padded with the specified fill character (default is space) to reach the given width. This is useful for formatting output in tables or console applications where you want consistent column widths.

Syntax

str.ljust(width, fillchar=' ')

Parameters

ParameterTypeDefaultDescription
widthintRequiredMinimum length of the returned string
fillcharstr’ ‘Character used for padding

Returns: A new string of at least width length, left-justified.

Examples

Basic usage with spaces

>>> s = "hello"
>>> s.ljust(10)
'hello     '
>>> len(s.ljust(10))
10

The string “hello” is 5 characters long. When you ask for a width of 10, Python adds 5 spaces to the right to reach the target length.

Custom fill character

>>> "Py".ljust(8, '*')
'Py******'
>>> "Code".ljust(10, '-')
'Code------'

You can use any character for padding, not just spaces. This is handy for creating visual separators.

Width shorter than string

>>> "hello".ljust(3)
'hello'

If the specified width is less than or equal to the string’s length, the original string is returned unchanged.

Practical formatting example

>>> data = [("Name", "Alice"), ("Age", "30"), ("City", "NYC")]
>>> for key, value in data:
...     print(f"{key.ljust(10)}: {value}")
Name      : Alice
Age       : 30
City      : NYC

This creates nicely aligned key-value output where all keys start at the same column position.

In formatted tables

>>> headers = ["Product", "Price", "Qty"]
>>> rows = [["Apple", "1.50", "10"], ["Banana", "0.75", "50"]]
>>> for row in rows:
...     print(" | ".join(cell.ljust(10) for cell in row))
Apple      | 1.50       | 10        
Banana     | 0.75       | 50        

With f-strings (modern alternative)

>>> f"{'hello':<10}"
'hello     '

Note that f-strings provide a more concise syntax for string alignment in Python 3.6+. The < specifier means left-align.

Common Patterns

Aligning dictionary output

>>> d = {"name": "Alice", "age": "30", "city": "London"}
>>> max_key_len = max(len(k) for k in d)
>>> for key, value in d.items():
...     print(f"{key.ljust(max_key_len)} : {value}")
name : Alice
age  : 30
city : London

Creating fixed-width columns for logs

>>> levels = ["INFO", "WARNING", "ERROR"]
>>> messages = ["Starting up", "Low memory", "Connection failed"]
>>> for level, msg in zip(levels, messages):
...     print(f"[{level.ljust(8)}] {msg}")
[INFO    ] Starting up
[WARNING ] Low memory
[ERROR   ] Connection failed

Errors

  • If fillchar is not exactly one character, TypeError is raised
  • If width is not an integer, TypeError is raised
>>> "hello".ljust(10, "ab")  # fillchar must be single char
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ljust() argument 2 must be str, not int

>>> "hello".ljust("10")  # width must be int
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'int'

See Also