Print a string at a fixed width in Python

avatar

Borislav Hadzhiev

Last updated: Sep 3, 2022

banner

Photo from Unsplash

Print a string at a fixed width in Python #

Use a formatted string literal to print a string at a fixed width, e.g. print(f'{my_str: <6}'). You can use an expression inside of a formatted string literal where you can specify the width of the string and the alignment.

main.py
my_str = 'hi' # ✅ print string with fixed width of 6 (left-aligned) result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # -------------------------------------------------- # ✅ print string with fixed width of 6 (right-aligned) result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi' # -------------------------------------------------- result = f'{my_str: <4}world' print(repr(result)) # 👉️ 'hi world' # -------------------------------------------------- # 👇️ if you have the width stored in a variable width = 6 result = f'{my_str: <{width}}' print(repr(result)) # 👉️ 'hi '

We used a formatted string literal to print a string at a fixed width.

Notice that I used the repr() function to also print the string's quotes to better illustrate how the string gets padded with spaces.

You can remove the call to repr() if you don't need it.

main.py
result = f'{my_str: >6}' # 👇️ with repr(), shows quotes print(repr(result)) # 👉️ ' hi' # 👇️ without repr(), doesn't show quotes print(result) # 👉️ hi

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

main.py
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # 👉️ is subscribed: True

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

main.py
my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'

The space between the colon and the less-than sign is the fill character.

main.py
my_str = 'hi' result = f'{my_str:.<6}' print(repr(result)) # 👉️ 'hi....' result = f'{my_str:.>6}' print(repr(result)) # 👉️ '....hi'

The example above uses a period as the fill character instead of a space.

The less-than or greater-than sign is the alignment.

The less-than sign aligns the string to the left and the greater-than sign aligns the string to the right.

The digit after the sign is the width of the string.

main.py
print(repr(f'{"hi": <6}')) # 👉️ 'hi ' print(repr(f'{"hii": <6}')) # 👉️ 'hii ' print(repr(f'{"hiii": <6}')) # 👉️ 'hiii ' print(repr(f'{"hiiii": <6}')) # 👉️ 'hiiii ' print(repr(f'{"hiiiii": <6}')) # 👉️ 'hiiiii'

Notice that I used single quotes to wrap the f-strings and double quotes inside of it.

It's important to alternate between single and double quotes to avoid terminating the f-string prematurely.

If you have the width of the string stored in a variable, make sure to wrap it in curly braces in the f-string.

main.py
width = 6 result = f'{my_str: <{width}}' print(repr(result)) # 👉️ 'hi '

Notice that we used an extra set of curly braces to evaluate the width variable after the less-than sign.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.