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 Top answer 1 of 16
3511
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.
2 of 16
506
Just use the rjust method of the string object.
This example creates a 10-character length string, padding as necessary:
>>> s = 'test'
>>> s.rjust(10, '0')
>>> '000000test'
Videos
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
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.
Saralgyaan
saralgyaan.com › posts › f-string-in-python-usage-guide
Python f-strings - The Ultimate Usage Guide
August 23, 2021 - We cannot provide a description for this page right now
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.
Top answer 1 of 5
216
Make use of the zfill() helper method to left-pad any string, integer or float with zeros; it's valid for both Python 2.x and Python 3.x.
It important to note that Python 2 is no longer supported.
Sample usage:
print(str(1).zfill(3))
# Expected output: 001
Description:
When applied to a value, zfill() returns a value left-padded with zeros when the length of the initial string value less than that of the applied width value, otherwise, the initial string value as is.
Syntax:
str(string).zfill(width)
# Where string represents a string, an integer or a float, and
# width, the desired length to left-pad.
2 of 5
185
Since python 3.6 you can use f-string :
>>> length = "1"
>>> print(f'length = {length:03}')
length = 100
>>> print(f'length = {length:>03}')
length = 001
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.