Pad a string to a fixed length with spaces in Python

avatar

Borislav Hadzhiev

Last updated: Sep 5, 2022

banner

Photo from Unsplash

Pad a string to a fixed length with spaces in Python #

Use a formatted string literal to pad a string to a fixed length with spaces, e.g. result = f'{my_str: <8}'. You can use an expression inside of a formatted string literal where you can specify the length of the string and the alignment.

main.py
# ✅ print multiple variables on separate lines (sep) my_str = 'bobby' # ✅ pad string to fixed length with spaces (left-aligned) result = f'{my_str: <8}' print(repr(result)) # 👉️ 'bobby ' # ------------------------------------------------- # ✅ pad string to fixed length with spaces (right-aligned) result = f'{my_str: >8}' print(repr(result)) # 👉️ ' bobby' # ------------------------------------------------- # ✅ pad string to fixed length with spaces (centered) result = f'{my_str:^8}' print(repr(result)) # 👉️ ' bobby '

We used a formatted string literal to pad a string to a fixed length with spaces.

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.

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

main.py
name = 'bobbyhadz' extension = '.com' result = f'{name}{extension}' print(result) # 👉️ bobbyhadz.com

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 = 'bobby' result = f'{my_str: <8}' print(repr(result)) # 👉️ 'bobby '

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

main.py
my_str = 'bobby' result = f'{my_str:.<8}' print(repr(result)) # 👉️ 'bobby...'

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.

If you need to pad the string with leading spaces to a fixed length, use a greater-than sign.

main.py
my_str = 'bobby' result = f'{my_str: >8}' print(repr(result)) # 👉️ ' bobby'

If you need to center the string, use a caret ^ symbol.

main.py
my_str = 'bobby' result = f'{my_str:^8}' print(repr(result)) # 👉️ ' bobby '

Alternatively, you can use the str.ljust() method.

Pad a string to a fixed length with spaces using str.ljust() #

Use the str.ljust() method to pad a string to a fixed length with spaces, e.g. my_str.ljust(8). The str.ljust() method will pad the string with the specified number of spaces and will return the result.

main.py
my_str = 'bobby' result = my_str.ljust(8) print(repr(result)) # 👉️ 'bobby '

The str.ljust method pads the end of the string to the specified width with the provided fill character.

The str.ljust method takes the following 2 arguments:

NameDescription
widthThe total length of the padded string
fillcharThe fill character to pad the string with

The default fill character is a space, so we didn't have to pass the second argument in the call to ljust().

There is also a str.rjust() method if you need to pad the string with leading spaces to a fixed length.

main.py
my_str = 'bobby' result = my_str.rjust(8) print(repr(result)) # 👉️ ' bobby'

The str.rjust method pads the beginning of the string to the specified width with the provided fill character.

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.