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.
Answer from nyedidikeke on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_string_zfill.asp
Python String zfill() Method
Python Examples Python Compiler ... Q&A Python Training ... The zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length....
People also ask

Should I use zfill or an f-string?
Use zfill for an existing string and sign-aware text padding; use an f-string when the source is numeric and the complete output format is known.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python zfill(): zero-pad strings, numbers, and signs
Python zfill(): Zero-Pad Strings, Numbers, and Signs
How do I add leading zeros in Python?
Convert the value to a string and call value.zfill(width), or use a numeric format such as f'{number:05d}' when formatting a number.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python zfill(): zero-pad strings, numbers, and signs
Python zfill(): Zero-Pad Strings, Numbers, and Signs
🌐
Codecademy
codecademy.com › docs › python › strings › .zfill()
Python | Strings | .zfill() | Codecademy
November 19, 2023 - The .zfill() method pads a string with zeros on the left to maintain a specific length.
🌐
Tutorialspoint
tutorialspoint.com › python › string_zfill.htm
Python String zfill() Method
The Python String zfill() method is used to fill the string with zeroes on its left until it reaches a certain width; else called Padding. If the prefix of this string is a sign character (+ or -), the zeroes are added after the sign character
🌐
GeeksforGeeks
geeksforgeeks.org › python-string-zfill
Python String zfill() - GeeksforGeeks
January 2, 2025 - The zfill(3) method pads the string with two zeros on the left to make its total length 3. What happens when the string length matches the specified width? ... The string "12345" already has a length of 5. No padding is added because the string ...
Find elsewhere
🌐
Programiz
programiz.com › python-programming › methods › string › zfill
Python String zfill()
The zfill() method returns a copy of the string with '0' characters padded to the left.
🌐
Python Pool
pythonpool.com › home › tutorials › python zfill(): zero-pad strings, numbers, and signs
Python zfill(): Zero-Pad Strings, Numbers, and Signs
July 13, 2026 - Use Python str.zfill() to add leading zeros, preserve plus and minus signs, and choose the right width for IDs and reports.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › str › zfill.html
zfill — Python Reference (The Right Way) 0.1 documentation
>>> '350'.zfill(1) '350' >>> '350'.zfill(2) '350' >>> '350'.zfill(3) '350' >>> '350'.zfill(4) '0350' >>> '350'.zfill(10) '0000000350' >>> '350'.zfill(20) '00000000000000000350' >>> '-350'.zfill(5) '-0350' #TODO
🌐
AskPython
askpython.com › python › string › python-string-zfill
Python string zfill() - AskPython
August 6, 2022 - The Python string zfill() method is used to pad a string with zeros on the left until a specific width is reached. This is the most "Pythonic" way to add
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › methods › string › zfill › python-string-zfill
Python zfill() function | Why do we use Python String zfill() function? |
September 27, 2021 - The Python zfill() function is a string method that returns a copy of the string by padding 0s to the left of the input string. Python zfill() is a built-in string method that pads the‘ 0’ character to the left of the input string and returns it.
🌐
Javatpoint
javatpoint.com › python-string-zfill-method
Python String | zfill() method with Examples - Javatpoint
We are excited to announce that we are moving from JavaTpoint.com to TpointTech.com on 10th Feb 2025. Stay tuned for an enhanced experience with the same great content and even more features. Thank you for your continued support · Python zfill() method fills the string at left with 0 digit ...
🌐
Scaler
scaler.com › home › topics › zfill() in python | python string zfill() method
zfill() in Python | Python String zfill() Method - Scaler Topics
March 15, 2022 - The zfill() method in Python Programming returns a String by adding zeros (0) to the left of the string to reach the specified length that is mentioned in its parameters.
🌐
Initial Commit
initialcommit.com › blog › python-zfill-method
Python zfill Method
November 1, 2021 - zfill() is a built-in string method in Python that is used for padding zeros to the left side of the string.
🌐
datagy
datagy.io › home › python posts › python strings › python zfill & rjust: pad a string in python
Python zfill & rjust: Pad a String in Python • datagy
December 16, 2022 - In this tutorial, you’ll learn how to use Python’s zfill method to pad a string with leadering zeroes. You’ll learn how the method works and how to zero pad a string and a number. You’ll also learn how to use the method in Pandas as well as how to use sign prefixes, such as +
🌐
iO Flood
ioflood.com › blog › python-zfill
Python's 'zfill()' Method | Guide To Padding Numeric Strings
February 14, 2024 - The zfill method doesn’t modify the original string. Instead, it returns a new string with the padded zeros. This is because strings in Python are immutable, meaning they can’t be changed after they’re created.
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.14.7 documentation
See also ljust() and zfill(). ... Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator.