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 - This is the most common way to use padding format, right padding with spaces. name = 'Bill Doe' balance = 13.566print(f"- {'Name':<8}: {name}") print(f"- {'Balance':8}: {balance}")>>> - Name : Bill Doe >>> - Balance : 13.566 ยท The < indicate ...
Discussions

Allow f-string to dynamically pad spaces
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 ... More on discuss.python.org
๐ŸŒ discuss.python.org
0
June 27, 2023
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
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
What are some cool f-string tricks that you've learned?
The = sign modifier that prints the variable name. >>> foo=3 >>> print(f"{foo=}") foo=3 More on reddit.com
๐ŸŒ r/learnpython
59
202
February 14, 2024
๐ŸŒ
Fstring
fstring.help
fstring.help: Python f-string guide
An !s suffix to convert to a string explicitly is also supported, though often not needed (as this is the default behavior in most cases): ... By default, values are formatted to take up only as many characters as needed to represent the content. It is however also possible to define that a value should be padded to a specific length.
๐ŸŒ
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 ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ inputoutput.html
7. Input and Output โ€” Python 3.14.3 documentation
The string type has some methods that perform useful operations for padding strings to a given column width.
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_string_formatting.asp
Python String Formatting
To format values in an f-string, add placeholders {}, a placeholder can contain variables, operations, functions, and modifiers to format the value. ... A placeholder can also include a modifier to format the value.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-f-string
Python f-string: A Complete Guide | DataCamp
December 3, 2024 - F-strings are string literals prefixed with 'f' or 'F' that contain expressions inside curly braces {}. These expressions are evaluated at runtime and then formatted using the __format__ protocol. Unlike traditional string formatting methods, f-strings provide a more straightforward and readable way to embed Python expressions directly within string literals.
๐ŸŒ
CoenRaets
jsonviewer.ai โ€บ python-f-string-padding
Python f String Padding[Complete Guide with Examples] - JSON Viewer
July 5, 2023 - Here, the ^ character specifies centre-alignment, and the total width for each value is 10 with right-padding with spaces. In conclusion, Pythonโ€™s f-string padding is a helpful tool that lets you prepare output strings with precise widths and alignments.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-pad-python-strings-with-custom-characters-419450
How to pad Python strings with custom characters | LabEx
String padding is a technique used to modify the length of a string by adding characters to its beginning or end. This process helps create uniform string lengths, which is crucial in various programming scenarios such as formatting ...
๐ŸŒ
Real Python
realpython.com โ€บ python-f-strings
Python's F-String for String Interpolation and Formatting โ€“ Real Python
November 30, 2024 - Python's f-strings provide a readable way to interpolate and format strings. They're readable, concise, and less prone to error than traditional string interpolation and formatting tools, such as the .format() method and the modulo operator (%). F-strings are also faster than those tools!
๐ŸŒ
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 - If you are in a hurry, below are some quick examples of how to pad or fill spaces to the string to get a specific length of a string. # Quick Examples of Padding Spaces to String # Consider the string string1 = "PYTHON" # Example 1: Pad right using rjust() padded_string = string1.rjust(10) # Example 2: Pad right with padding character padded_string = string1.rjust(10, ' ') # Example 3: Pad left using ljust() padded_string = string1.ljust(10, ' ') # Example 4: Pad left using f-strings padded_string = f"{string1: >10}" # Example 5: Pad right using f-strings padded_string = f"{string1: <10}" # Example 6: Pad left using format() padded_string = '{: >10}'.format(string1) # Example 7: Pad right using format() padded_string = '{: <10}'.format(string1)
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python pad string with zeros
Python Pad String With Zeros - Spark By {Examples}
May 21, 2024 - Second example โ€“ 4 zeros are Right padded by specifying the value as 10. In both outputs, the length of the final string is 10, as the value is 10. Finally, format() in python is used to display the strings in the desired formats similar to f-strings. It can be used to pad zeros at Left and Right with > and < operators.
๐ŸŒ
Real Python
realpython.com โ€บ how-to-python-f-string-format-float
How to Format Floats Within F-Strings in Python โ€“ Real Python
April 24, 2024 - When you use .format(), itโ€™s also possible to define the format specifier dynamically at runtime. Suppose you have several words that you want to pad out to a width of four characters greater than their length. The code below shows you how to achieve this: ... >>> text = "Python is cool" >>> padded_words = [ ...
๐ŸŒ
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 - 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:
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ languages โ€บ python f-strings
Python F-Strings
August 22, 2023 - F-strings can also be used to format numbers in various ways, including rounding, padding, and adding prefixes or suffixes.
๐ŸŒ
Built In
builtin.com โ€บ data-science โ€บ python-f-string
Guide to String Formatting in Python Using F-strings | Built In
F-string is a way to format strings in Python. It was introduced in Python 3.6 and aims to make it easier for users to add variables, comma separators, do padding with zeros and date format.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ padding-strings-in-python
Padding Strings in Python
September 18, 2023 - These placeholders accept a variety of formatting options. Let's see how we can achieve different types of string padding by using these options: Left Padding: Use > inside the placeholder and a number to specify the desired width, to right align a string (append characters at the start): txt = "We {:>8} Python." print(txt.format('love'))