How to Pad Text to a Fixed Width in a Python f-string
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
f"{<value>:<align><width>}"
| Part | What 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
for name in ["Ada", "Grace", "Linus"]:
print(f"[{name:<8}] [{name:>8}] [{name:^8}]")

Step 2: Run It
uv run python padding.py
[Ada ] [ Ada] [ Ada ]
[Grace ] [ Grace] [ Grace ]
[Linus ] [ Linus] [ Linus ]

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
| Mistake | What happens | Do this instead |
|---|---|---|
| A value longer than the width | It isn't cut: f"[{'Christopher':<8}]" gave [Christopher], so that row is out of line | Make 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
- Format Specification Mini-Language — alignment, fill and width.
- f-strings — the language reference.
