You can do this with str.ljust(width[, fillchar]):

Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than len(s).

>>> 'hi'.ljust(10)
'hi        '
Answer from Felix Kling on Stack Overflow
🌐
Medium
medium.com › @johnidouglasmarangon › padding-f-strings-in-python-977b17edbd36
Padding f-strings in Python
February 18, 2022 - number = 2022 padding = 10print(f"{'decimal':<{padding}}{number:d}") print(f"{'octal':<{padding}}{number:o}") print(f"{'hex':<{padding}}{number:X}") print(f"{'binary':<{padding}}{number:b}")>>> decimal 2022 >>> octal 3746 >>> hex 7E6 >>> binary 11111100110 ... This is a way to use a set of format strings that will be interpreted together.
Discussions

f-string and string right padding is driving me crazy
Your code works for me. >>> header = "foo" >>> endpoint = "bar" >>> ename = f'"{endpoint}"' >>> print(f'{header} {ename:<30}!') foo "bar"                         ! More on reddit.com
🌐 r/learnpython
10
10
January 11, 2025
How can I dynamically pad a number in python f-strings? - Stack Overflow
I have an f-string like this: f"foo-{i:03d}" This is producing a filename for a file that I read in. The problem is sometimes there are hundreds of files, so the files are named like foo... More on stackoverflow.com
🌐 stackoverflow.com
Using an f-string with multiple parameters (decimal places plus string padding)
f"{x:<5.0f}" should do it. You can't directly chain the colons like you tried. You can actually put entire fstrings inside of other fstrings, but I wouldn't recommend it. More on reddit.com
🌐 r/learnpython
12
4
March 31, 2025
Strange behavior: leading zeros in formatted print function are reversed
It's because m is the string '4' not the integer 4. The default for strings is left align, the default for integers is right align. You can either convert m and d to integers m = int(m) d = int(m) or you can specify the alignment direction explicitly print(f"{y}-{m:>02}-{d:>02}") More on reddit.com
🌐 r/learnpython
4
7
September 3, 2024
🌐
Python.org
discuss.python.org › python help
Allow f-string to dynamically pad spaces - Python Help - Discussions on Python.org
June 27, 2023 - Example: # This works >>> print(f"{3:4}") '3 ' # However this does not work. >>> pad_amount: int = ... >>> print(f"3:pad_amount") ValueError: Unknown format code 'a' for object of type 'int' # I know this is the intended behavior, however the only other way to do this (or if you have better ways to do this please let me know) is using eval()
🌐
Peterbe.com
peterbe.com › plog › how-to-pad-fill-string-by-variable-python
How to pad/fill a string by a variable in Python using f-strings - Peterbe.com
January 24, 2020 - Friday, Jan 24, 2020 9 comments Python · I often find myself Googling for this. Always a little bit embarrassed that I can't remember the incantation (syntax). Suppose you have a string mystr that you want to fill with with spaces so it's 10 characters wide: >>> mystr = 'peter' >>> mystr.ljust(10) 'peter ' >>> mystr.rjust(10) ' peter' Now, with "f-strings" you do: >>> mystr = 'peter' >>> f'{mystr:<10}' 'peter ' >>> f'{mystr:>10}' ' peter' What also trips me up is, suppose that the number 10 is variable.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-pad-a-string-to-a-fixed-length-with-spaces-in-python
How to Pad a String to a Fixed Length with Spaces in Python - GeeksforGeeks
July 23, 2025 - When we are using Python 3.6 or ... the length directly in the f-string. ... The f"{a:<10}" f-string pads the string a to the right with spaces until it reaches 10 characters....
🌐
CoenRaets
jsonviewer.ai › python-f-string-padding
Python f String Padding[Complete Guide with Examples] - JSON Viewer
July 5, 2023 - Normally, empty spaces are added ... of zeros. You can specify padding in an f-string by using the characters “>” and “<” specify the direction of the padding, and a number to specify the width of the padded value....
🌐
Reddit
reddit.com › r/learnpython › f-string and string right padding is driving me crazy
r/learnpython on Reddit: f-string and string right padding is driving me crazy
January 11, 2025 -

I know this is dumb, but something is strange. Everything works from the python3 interpreter but in my code it doesn't work. All I want is a string to be padded to the right.

This doesn't work

            ename = f'"{endpoint}"'
            print(f'{header} {ename:<30}!') 

I always get a single space before the ! I expect to get this (which I get in the interactive mode)

HEADER ENAME....................!

(note: I put dots there to make the spaces clear, they should be spaces!)

But I get

HEADER ENAME.!

If I change it to this, it works

ename = f'"{endpoint}"'
print(f'{header} {ename:!<30}!')

But the padding is ! marks. (which was a test). If I change the ! to a space, that doesn't work.

Clearly I'm being dumb and missing something important. What an I missing? Thanks.

UPDATE: Appears to be caused by some post processing after python runs. Not sure what it is, but I don't believe it's python at the moment. Strange indeed!

UPDATE2: Well, I'm dumb. Problem was that this ultimately went through an HTML renderer and that would remove multiple spaces (exactly as it should). So 100% not a python issue. Thanks to all.

Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › pad-or-fill-a-string-by-a-variable-in-python-using-f-string
Pad or fill a string by a variable in Python using f-string - GeeksforGeeks
July 15, 2025 - If we want to pad a string to the right, we can align it to the left and use a variable for the padding character.
🌐
GeeksforGeeks
geeksforgeeks.org › python › add-padding-to-a-string-in-python
Add padding to a string in Python - GeeksforGeeks
July 23, 2025 - Padding a string involves adding ... methods to add padding to a string in Python. F-strings allow us to specify padding directly within the string using alignment specifiers....
🌐
ZetCode
zetcode.com › python › fstring
Python f-string - formatting strings in Python with f-string
May 11, 2025 - $ python main.py Fixed format: 123.457 Adaptive format: 12345.7 · F-strings support custom fill characters for padding when aligning text. Instead of just spaces or zeros, you can use any character to fill the extra space, which is useful for creating visual separators or decorative output.
🌐
EyeHunts
tutorial.eyehunts.com › home › python f string padding | example code
Python f string padding | Example code - EyeHunts
September 11, 2021 - Python f string padding With the introduction of formatted string literals ("f-strings" for short) in Python 3.6, it is to access previously defined variables with a briefer syntax:
🌐
Bomberbot
bomberbot.com › python › mastering-string-padding-in-python-the-art-of-f-strings
Mastering String Padding in Python: The Art of F-Strings - Bomberbot
Here, value represents the string or variable to be padded, fill is the optional character used for padding (defaulting to a space), align specifies the alignment (< for left, > for right, ^ for center), and width determines the total width of the resulting string.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › pad string with spaces in python
Pad String with Spaces in Python - Spark By {Examples}
May 21, 2024 - How to pad the string with spaces in Python? Adding some characters to the string is known as Padding or filling and is useful when you wanted to make a
🌐
Real Python
realpython.com › how-to-python-f-string-format-float
How to Format Floats Within F-Strings in Python – Real Python
March 18, 2026 - You achieve this by nesting the precision replacement field {length} within the format specifier. The asterisk (*) defines the padding character to be used, while the caret (^) symbol ensures the output is centered.
🌐
LernerPython
lernerpython.com › home › lernerpython blog › python f-string format specifiers: aligning and padding text
Python f-string format specifiers: Aligning and padding text - LernerPython
April 29, 2026 - Use in f-string format specifiers, with a width integer and a custom fill character, to left-align, center or right-align and pad text.
🌐
Built In
builtin.com › data-science › python-f-string
Guide to String Formatting in Python Using F-strings | Built In
Summary: Python f-strings, introduced in version 3.6, allow cleaner string formatting by embedding variables directly. They support alignment, zero-padding, decimal precision, percentage display, comma separators and date formatting, making ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-padding-a-string-upto-fixed-length
Python | Padding a string upto fixed length - GeeksforGeeks
March 23, 2023 - Use the 'f-string' with format specifier '{:<N}' to left justify the 'ini_string' and pad it with spaces on the right side upto 'padding_size' length. Store the resulting string in a new variable 'res'.
🌐
Python Morsels
pythonmorsels.com › string-formatting
Python f-string tips & cheat sheets - Python Morsels
April 12, 2022 - The Nd format specifier (where N is a whole number) will pad an integer with space characters until it's N digits long.