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
a = "hello" b = "welcome to the jungle" c = "10.000" print(a.zfill(10)) print(b.zfill(10)) print(c.zfill(10)) Try it Yourself » ... Coding fundamentals as bite-sized lessons and challenges. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.zfill.html
pandas.Series.str.zfill — pandas 3.0.5 documentation - PyData |
Note that 10 and NaN are not strings, therefore they are converted to NaN. The minus sign in '-1' is treated as a special character and the zero is added to the right of it (str.zfill() would have moved it to the left).
🌐
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
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python-string-zfill
Python String zfill() - GeeksforGeeks
January 2, 2025 - zfill() method in Python is used to pad a string with zeros (0) on the left until it reaches a specified width.
Find elsewhere
🌐
AskPython
askpython.com › python › string › python-string-zfill
Python string zfill() - AskPython
August 6, 2022 - Otherwise, since Python strings are immutable, this function returns a new string. ... If there is a leading sign (+/-), the padding is added after the sign, maintaining the position of the leading sign even in the new string. Let’s look at some examples now. If there is no leading sign, the zeros are padded to the left. >>> a = "AskPython" >>> a.zfill(15) '00000AskPython'
🌐
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'
🌐
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 - Quick answer: zfill(width) returns a new string padded with zeros on the left. If the string begins with + or -, Python places the zeros after the sign.
🌐
Scaler
scaler.com › home › topics › zfill() in python | python string zfill() method
zfill() in Python | Python String zfill() Method - Scaler Topics
March 15, 2022 - This zfill() function in Python for width greater than string length will always return the copy of the original string with zeros added to the left side.
🌐
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.
🌐
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.
🌐
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.
🌐
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 ...
🌐
w3resource
w3resource.com › pandas › series › series-str-zfill.php
Pandas Series: str.zfill() function - w3resource
May 20, 2026 - Pandas Series: str.zfill() function: The str.zfill() function is used to pad strings in the Series/Index by prepending '0' characters.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Pad Strings and Numbers with Zeros in Python (Zero-padding) | note.nkmk.me
May 18, 2023 - To pad an integer with zeros, first convert it to a string using str(), then call zfill().