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
🌐
GeeksforGeeks
geeksforgeeks.org › python › add-padding-to-a-string-in-python
Add padding to a string in Python - GeeksforGeeks
July 23, 2025 - The :> specifier right-aligns the string, and 10 determines the total width. textwrap module provides advanced formatting features and can be used for padding in more complex scenarios.
Discussions

String Format Padding
You want to pad with zeros instead of spaces (default behavior). The documentation is totally not clear about it. Best too google stack overflow for these types of questions "python pad leading zero". >>> # pad with 0 so width is 2 >>> print "%02d" % 1 01 >>> # pad with 0 so width is 3 >>> print "%03d" % 5 005 More on reddit.com
🌐 r/learnpython
2
1
December 12, 2014
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
2
0
June 27, 2023
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 easily pad a float with x zeros on the left and round/pad to y decimal places on the right?
I think this should work: "%05.2f". It's padding to a minimum overall length of 5 (which includes the decimal point) and pads with 0 instead of spaces. More on reddit.com
🌐 r/godot
6
2
May 8, 2023
🌐
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 ...
🌐
w3resource
w3resource.com › python › python-format.php
Python String Formatting
April 14, 2026 - In %-style you usually use %s for the string representation but there is %r for a repr(...) conversion. ... class Data(object): def __str__(self): return 'str' def __repr__(self): return 'repr' x='{0!s} {0!r}'.format(Data()) print (x) ... In Python 3 there exists an additional conversion flag that uses the output of repr(...) but uses ascii(...) instead. ... A value can be padded to a specific length.
🌐
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'))
🌐
LabEx
labex.io › tutorials › python-how-to-pad-python-strings-with-custom-characters-419450
How to pad Python strings with custom characters | LabEx
## Complex padding scenario def format_currency(amount): return f"${str(amount).rjust(10, ' ')}" print(format_currency(42.50)) ## Output: $ 42.50 print(format_currency(1234.56)) ## Output: $ 1234.56 · Choose the appropriate method based on your specific requirements · Consider the context and readability of padded strings · Be mindful of performance with large-scale string manipulations · By mastering these padding methods, developers can create more structured and visually consistent string representations in Python.
🌐
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
In Python 3 there exists an additional conversion flag that uses the output of repr(...) but uses ascii(...) instead. class Data(object): def __repr__(self): return 'räpr' ... 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.
Find elsewhere
🌐
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
🌐
STechies
stechies.com › padding-strings-python
How to add Padding to Python Strings
September 21, 2022 - Padding strings in Python is one of the most standard and common practices for Python users. There are certain functions in Python that helps in string padding. These are .ljust(), .rjust(), .center(), .zfill(), .format() functions, f-strings, etc.
🌐
Reddit
reddit.com › r/learnpython › string format padding
r/learnpython on Reddit: String Format Padding
December 12, 2014 -

Hi there. I'd like some help on the following.

What I'm trying to output

_8:30_PM

note the underscore is a space because of reddit's markdown. It's representing a space

What I know

return '%2d:%2d PM' %(hour,minute)

If the minute is 2 i have to pad it with 02. If the hour is 3 I have to pad it to _3.

I don't know how to do the latter (pad the minute)

🌐
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()
🌐
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 - Padding strings is useful for formatting data, aligning text, or preparing strings for output. The simplest and most common way to pad a string in Python is by using the built-in ljust() and rjust() methods.
🌐
Medium
medium.com › pythons-gurus › how-padding-works-in-python-and-how-to-use-it-8c0020830f8d
How Padding Works in Python and How to Use It | Python’s Gurus
December 5, 2024 - Python provides built-in methods to pad strings to a desired width: str.ljust(width, char): Left-aligns the string and pads it with the specified character.
🌐
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....
🌐
GeeksforGeeks
geeksforgeeks.org › 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
January 2, 2025 - In Python, we can pad or fill a string with a variable character to make it a specific length using f-strings.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Pad Strings and Numbers with Zeros in Python (Zero-padding) | note.nkmk.me
May 18, 2023 - Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility. Built-in Types - printf-style String Formatting — Python 3.11.3 documentation ... i = 1234 print('Zero Padding: d' % i) # Zero Padding: 00001234 i = -1234 print('Zero Padding: d' % i) # Zero Padding: -0001234
🌐
Finxter
blog.finxter.com › home › learn python blog › python how to pad zeros to a string?
Python How to Pad Zeros to a String? - Be on the Right Side of Change
November 16, 2020 - You can modify the string inside the replacement field. You can pad, align and change its length. This is called format specification in Python, or format_spec for short. In an f-string, you specify this using a colon :. Everything after the colon is a formatting option.
🌐
Python Engineer
python-engineer.com › posts › pad-zeros-string
How to pad zeros to a String in Python - Python Engineer
zfill is the best method to pad zeros from the left side as it can also handle a leading '+' or '-' sign. It returns a copy of the string left filled with '0' digits to make a string of length width.
🌐
TutorialsPoint
tutorialspoint.com › how-can-i-fill-out-a-python-string-with-spaces
How can I fill out a Python string with spaces?
October 19, 2022 - In this article we are going to discuss fill out a Python string with spaces. Let?s see various solutions one by one. To pad the string with desired characters python provides there methods namely, ljust(), rjust() and center().