To pad strings:

>>> print('4'.zfill(3))
004
>>> print('-4'.zfill(3))
-04

To pad numbers:

>>> n = 4
>>> print(f'{n:03}') # Preferred method, python >= 3.6
004
>>> print('%03d' % n)
004
>>> print(format(n, '03')) # python >= 2.6
004
>>> print('{0:03d}'.format(n))  # python >= 2.6 + python 3
004
>>> print('{foo:03d}'.format(foo=n))  # python >= 2.6 + python 3
004
>>> print('{:03d}'.format(n))  # python >= 2.7 + python3
004

String formatting documentation.

Answer from Harley Holcombe on Stack Overflow
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python pad string with zeros
Python Pad String With Zeros - Spark By {Examples}
May 21, 2024 - # Use zfill() to pad zeros print(string1.zfill(10)) # Output: # 0000PYTHON · The f-strings in python are used to display the strings in the desired formats.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-pad-a-string-to-a-fixed-length-with-zeros-in-python
How to Pad a String to a Fixed Length with Zeros in Python - GeeksforGeeks
July 23, 2025 - We can specify the total length and use zeros to pad the string. ... In the f-string f"{a:0>6}", 0 is the padding character, > means to pad from the left, and 6 is the total length.
🌐
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 the right orientation, you can omit it, because it is a default value. The first print it is explicit and on the second one is omitted. ... Just use> to indicate the orientation. name = 'Bill Doe' balance = 13.566print(f"- {name:>12}") print(f"- {balance:>12}")>>> - Bill Doe >>> - 13.566
🌐
Python Engineer
python-engineer.com › posts › pad-zeros-string
How to pad zeros to a String in Python - Python Engineer
Converted a number to a formatted string with leading zeros by using string formatting: n = 42 print(f'{n:05d}') print('d' % n) print('{0:05d}'.format(n)) # --> all will output the same: # '00042' # '00042' # '00042'
🌐
Note.nkmk.me
note.nkmk.me › home › python
Pad Strings and Numbers with Zeros in Python (Zero-padding) | note.nkmk.me
May 18, 2023 - Right-justify, center, left-justify strings and numbers in Python · For these methods, the first argument is the length of the string to be returned, and the second argument is '0' for zero-padding.
🌐
CoenRaets
jsonviewer.ai › python-f-string-padding
Python f String Padding[Complete Guide with Examples] - JSON Viewer
July 5, 2023 - Use of zeros or spaces as padding is possible. Normally, empty spaces are added before or after values to align them, whereas leading zeros are added to numerical values by using spaces instead of zeros. You can specify padding in an f-string by using the characters “>” and “<” specify ...
🌐
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 - In Python, we can pad or fill a string with a variable character to make it a specific length using f-strings.
Find elsewhere
🌐
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 in for loop · for n in range(2): print(f'The number is {n:4}') Output: The number is 0 The number is 1 · Do comment if you have any doubts and suggestions on this Python f string tutorial. Note: IDE: PyCharm 2021.3.3 (Community Edition) Windows 10 ·
🌐
Medium
medium.com › vicipyweb3 › mastering-leading-zero-magic-a-friendly-guide-to-formatting-leading-zeros-in-python-3cb934d7aa4f
Mastering Leading Zero Magic: A Friendly Guide to Formatting Leading Zeros in Python | by PythonistaSage | ViciPyWeb3 | Medium
May 3, 2023 - The f-string formats it with a minimum width of 2 characters, padding with a leading zero to get the result '07'. If you have a number with more digits than the specified width, it won’t be truncated or altered.
🌐
Built In
builtin.com › data-science › python-f-string
Guide to String Formatting in Python Using F-strings | Built In
Now, it’s time to use what we’ve learned in the previous section to format numbers with zeros to the left/right. As we’ve seen before, we can pick the padding character. In this case, we’ll choose the 0 as our padding character to have five decimals in our float below.
🌐
Python Morsels
pythonmorsels.com › string-formatting
Python f-string tips & cheat sheets - Python Morsels
April 12, 2022 - This fixed-point notation format specification rounds the number the same way Python's round function would. The 0Nd format specifier (where N is a whole number) will format a number to be N digits long (by zero-padding it on the left-hand side).
🌐
Plain English
python.plainenglish.io › get-better-at-python-f-strings-83b123bb4ce0
Get Better at Python f-strings. Make a coffee, sit back, and enjoy the… | by Görkem Arslan | Python in Plain English
July 10, 2021 - This article is not a tutorial about f-strings. Indeed, this is a guide to improve your skills a little bit further while using f-strings. You can pad any number with zeros by using f-strings.
🌐
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
October 19, 2022 - 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. It will not be printed to the screen. Let’s look at the syntax to pad, align and change the length of a string. We type the following in this order, after a colon : ... Example: So if we want to pad our string with zeros, i.e. fill 0, right align and have width 10, we would use
🌐
W3Schools
w3schools.com › python › ref_string_zfill.asp
Python String zfill() Method
The zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length. If the value of the len parameter is less than the length of the string, no filling is done.
🌐
Stack Abuse
stackabuse.com › bytes › add-leading-zeros-to-a-number-in-python
Add Leading Zeros to a Number in Python
July 2, 2022 - Here the 03 specifies that the string should be padded with 3 zeros. The second digit in this argument is equivalent to the width parameter in the zfill function. You might also notice that using f-strings, you don't need to explicitly convert your integer to a string, which it will do for you.
🌐
sqlpey
sqlpey.com › python › python-zero-padding-techniques
Python Zero Padding: String and Number Formatting Techniques …
January 2, 2025 - The standard format specifiers allow fine-grained control: [fill][align][sign][#][0][minimumwidth][.precision][type]. The 0 in 0<width> signifies zero-padding. ... >>> i = 1 >>> print(f"{i:0>2}") # Works for both numbers and strings. 01 >>> print(f"{i:02}") # Works only for numbers.