Skip to main content

How to Pad Text to a Fixed Width in a Python f-string

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

When you print a list of names and numbers, short and long names push the numbers out of line. Padding every name to the same width lines them up in columns. In an f-string you do it with :< and a width. This guide pads three names three ways on Python 3.15.2.

Syntax​

Syntax
f"{<value>:<align><width>}"
PartWhat it means
<value>The text or number to show
<align>< left, > right or ^ centre
<width>The total number of characters the value takes up, padded with spaces

The format specification documentation describes the three: < "Forces the field to be left-aligned within the available space (this is the default for most objects)", > "Forces the field to be right-aligned within the available space (this is the default for numbers)" and ^ "Forces the field to be centered within the available space."

The Example​

Three names of different lengths, each printed in square brackets so you can see the spaces, aligned left, right and centre in a width of 8.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

padding.py
for name in ["Ada", "Grace", "Linus"]:
print(f"[{name:<8}] [{name:>8}] [{name:^8}]")

The padding.py script in a macOS-style code window: a for loop over Ada, Grace and Linus that prints each name in brackets three times: left-aligned, right-aligned and centred in a width of 8.

Step 2: Run It​

Terminal
uv run python padding.py
Output
[Ada ] [ Ada] [ Ada ]
[Grace ] [ Grace] [ Grace ]
[Linus ] [ Linus] [ Linus ]

The terminal after uv run python padding.py: three lines where each name fills 8 characters, padded on the right in the first column, on the left in the second and on both sides in the third.

Every name now takes exactly 8 characters, so the brackets line up. <8 put the spaces after the name, >8 before it, and ^8 split them, with the extra space on the right when it couldn't split evenly. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
A value longer than the widthIt isn't cut: f"[{'Christopher':<8}]" gave [Christopher], so that row is out of lineMake the width as large as the longest value
Leaving out the width: {name:<}Nothing changes. The docs: "unless a minimum field width is defined, the field width will always be the same size as the data to fill it"Always give a number after the alignment

FAQ​

Can I pad with something other than spaces?​

Yes, put the character before the alignment sign: f"[{'Ada':*<8}]" gave [Ada*****].

How do I line up a number column next to the names?​

Pad the name and give the number its own width: f"{name:<10}{age:>4}". Numbers are right-aligned by default, which lines up their last digits.

Conclusion​

:<8, :>8 and :^8 padded each name to 8 characters on the right, on the left or on both sides, so the columns lined up.

Additional Resources​