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 ...
๐ŸŒ
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.

๐ŸŒ
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 ...
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.
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
Cheatography
cheatography.com โ€บ brianallan โ€บ cheat-sheets โ€บ python-f-strings-number-formatting
Python F-Strings Number Formatting Cheat Sheet by BrianAllan - Download free from Cheatography - Cheatography.com: Cheat Sheets For Every Occasion
Contains formulas, tables, and examples showing patterns and options focused on number formatting for Python's Formatted String Literals -- i.e., F-Strings.
Rating: 0 โ€‹ - โ€‹ 2 votes
๐ŸŒ
Base64.Guru
base64.guru โ€บ home โ€บ base64 converter โ€บ base64 decode
Base64 to Image | Base64 Decode | Base64 Converter | Base64
Thank you, my friend. This tool has allowed me to perform great things. ... Is there an API we can use to do this? I wanted to implement this in my automation where i have a base64 then convert it to image and download. Thanks! ... Hi, I want to do exactly this but in Python. I got my base64 string and I want to convert that into a PIL.Image type.
๐ŸŒ
DEV Community
dev.to โ€บ erictleung โ€บ print-fixed-fields-using-f-strings-in-python-26ng
Print fixed fields using f-strings in Python - DEV Community
August 20, 2020 - After searching a littleโ€ฆ You might consider adding links to (a) 2.4.3. Formatted string literals (docs.python.org/3/reference/lexical_analysis.html#f-strings) and/or (b) PEP 498 โ€“ Literal String Interpolation
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.6 documentation
See also index() and find(). ... Return the string right justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space).
๐ŸŒ
DartPad
dartpad.dev
DartPad
An online Dart editor with support for console and Flutter apps.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ whatsnew โ€บ 3.14.html
Whatโ€™s new in Python 3.14
Also added in 3.14: concurrent.futures.InterpreterPoolExecutor. ... Template strings are a new mechanism for custom string processing.
๐ŸŒ
Nickmccullum
nickmccullum.com โ€บ complete-guide-python-f-strings
The Complete Guide to Python f-Strings | Nick McCullum
October 8, 2020 - Python f-Strings are an improved method for formatting strings in python. It was introduced in Python 3.6 as a concise, faster, more readable and less error-prone method of formatting strings. F-Strings are formatted string literals that allows variables, functions and expressions to be embedded ...
๐ŸŒ
Real Python
realpython.com โ€บ python-formatted-output
A Guide to Modern Python String Formatting Tools โ€“ Real Python
February 1, 2025 - In modern Python, you have f-strings and the .format() method to approach the tasks of interpolating and formatting strings. These tools help you embed variables and expressions directly into strings, control text alignment, and use custom format specifiers to modify how values appear.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ CSS โ€บ Reference
CSS reference - CSS | MDN
Use this CSS reference to browse an alphabetical index of all of the standard CSS properties, pseudo-classes, pseudo-elements, data types, functional notations and at-rules. You can also browse key CSS concepts and a list of selectors organized by type. Also included is a brief DOM-CSS / CSSOM ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_strings_format.asp
Python - Format Strings
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... age = 36 #This will produce an error: txt = "My name is John, I am " + age print(txt) Try it Yourself ยป ยท But we can combine strings and numbers by using f-strings or the format() method!