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....
🌐
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 - 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
🌐
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
🌐
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
🌐
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 - 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.
🌐
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 - Series.str.zfill(self, width) Parameters: Returns: Series/Index of objects · Example: Python-Pandas Code: import numpy as np import pandas as pd s = pd.Series(['-2', '2', '2000', 20, np.nan]) s · Output: 0 -2 1 2 2 2000 3 20 4 NaN dtype: object · Note that 10 and NaN are not strings, therefore they are converted to NaN.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Pad Strings and Numbers with Zeros in Python (Zero-padding) | note.nkmk.me
May 18, 2023 - This article explains how to perform zero-padding on various data types such as strings (str) and integers (int) in Python. ... Specify the length of the returned string. The original string will be right-justified, and the left side will be filled with zeros. s = '1234' print(s.zfill(8)) # 00001234 print(type(s.zfill(8))) # <class 'str'>