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 ... Python Interview 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 following example shows how .zfill() method helps ensure a specific length for a string, adding zeros to the beginning if the string is shorter than the desired length. ... Learn to analyze and visualize data using Python and statistics.
Discussions

Getting ArcGIS to include leading zeros in text field - Geographic Information Systems Stack Exchange
Python has a built-in for leading zeros. Use str.zfill() to add leading zeros into a text field. More on gis.stackexchange.com
๐ŸŒ gis.stackexchange.com
August 10, 2017
Problem with leading zeros disappearing when reading an Excel file
Do you want to do mathematical operations on the numbers? If not, You can define the dtype of column as "str" while importing the data into pandas. More on reddit.com
๐ŸŒ r/learnpython
20
12
December 3, 2022
How would you add zeros to the beginning of an Integer, such that there are 4 total digits in the number?
zfill or format are not obscure at all. A python integer cannot have leading zeros. It's simply not possible. zfill and format work with strings. You can also use f-strings to add zeros to a string: data = 15 print(f"{data:0>4}") That said this sounds like an XY problem. We could help a lot more if you told us what problem you are trying to solve, not just how you are trying to solve it. What's the big picture here? http://xyproblem.info More on reddit.com
๐ŸŒ r/learnpython
4
2
February 16, 2022
pandas question: how can I preserve leading zeros after saving to a CSV?
The problem is: when I save it to a CSV, the leading zeros in one of my columns get dropped. How do you know? For instance, if you are opening the file in Excel, it might be that Excel is doing this trimming. Minimal example: >>> import pandas as pd >>> >>> # make a sample dataframe with numerical data >>> df = pd.DataFrame({'x': [1, 2, 300, 400, 500]}) >>> df.head() x 0 1 1 2 2 300 3 400 4 500 >>> >>> # convert the int column to str type >>> df['x'] = df['x'].astype(str) >>> >>> # add zero padding >>> df['x'] = df['x'].apply(lambda x: x.zfill(3)) >>> df.head() x 0 001 1 002 2 300 3 400 4 500 >>> >>> # save csv >>> df.to_csv('test.csv') After exiting python, I can check out the csv from terminal: $ cat test.csv ,x 0,001 1,002 2,300 3,400 4,500 the leading zeros are there. But Excel would remove them, assuming the column was numerical. So that might be your new problem to solve :) how to keep leading zeros within the CSV file itself? I bet they already ARE in the file itself I feel like I'm so close Maybe closer than you think, i.e. already done haha More on reddit.com
๐ŸŒ r/learnpython
15
5
February 17, 2021
People also ask

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
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
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-zfill
Python's 'zfill()' Method | Guide To Padding Numeric Strings
February 14, 2024 - Itโ€™s an essential tool for handling numeric strings in Python, especially when you need to ensure your strings are of a certain length. ... In this example, we have a string โ€™42โ€™ that we want to pad with zeros until itโ€™s five characters long. We use the zfill method with the argument 5, and it returns โ€˜00042โ€™. The number โ€™42โ€™ is now a string of five characters with leading zeros.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Pad Strings and Numbers with Zeros in Python (Zero-padding) | note.nkmk.me
May 18, 2023 - The original string will be ... the original string will be returned as it is. ... If a leading sign prefix (-, +) exists, the string is filled with zeros after the sign....
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-add-leading-zeros-to-string
Add Leading Zeros to String - Python - GeeksforGeeks
July 11, 2025 - Explanation: zfill() pads the string with zeros to reach the specified width. Here, 4 + len(s) ensures the string s is padded with 4 zeros. In this method, we are multiplying the 0 as a string with the number of zeros required and using Python ...
๐ŸŒ
Python Engineer
python-engineer.com โ€บ posts โ€บ pad-zeros-string
How to pad zeros to a String in Python - Python Engineer
>>> "42".zfill(5) '00042' >>> "-42".zfill(5) '-0042' If you want to insert an arbitrary character on the left side, use rjust. It returns a copy of the object right justified in a sequence of length width. The default filling character is a space. However, this does not handle a sign prefix. >>> "42".rjust(5) ' 42' >>> "42".rjust(5, "0") '00042' >>> "-42".rjust(5, "0") '00-42' Converted a number to a formatted string with leading zeros by using string formatting:
๐ŸŒ
Letpy
letpy.io โ€บ handbook โ€บ string-methods โ€บ zfill
The `zfill` Method in Python: Adding Leading Zeros to a String
Pads a string with zeros on the left to reach a specified minimum length. width -- desired minimum length of the resulting string. The original string is not shortened, even if it has fewer characters than the specified length. Leading + and - signs remain at the start of the string.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ add-leading-zeros-to-a-number-in-python
Add Leading Zeros to a Number in Python
July 2, 2022 - The parameter to zfill is the length of the string you want to pad, not necessarily the number of zeros to add. So in the example above, if your string is already 5 characters long, it will not add any zeros. ... No spam ever. Unsubscribe anytime. Read our Privacy Policy.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.Series.str.zfill.html
pandas.Series.str.zfill โ€” pandas 3.0.5 documentation - PyData |
Differs from str.zfill() which has special handling for โ€˜+โ€™/โ€™-โ€™ in the string. ... 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).
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python leading zeros
How to Display a Number With Leading Zeros in Python | Delft Stack
February 2, 2024 - The str.zfill() function is exclusively designed to pad a string with zeros towards its left side. It is an excellent choice and a highly recommended method in this case. The modulus % sign, sometimes even referred to as the string formatting ...
๐ŸŒ
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
๐ŸŒ
Linux Hint
linuxhint.com โ€บ python-pad-a-string-with-leading-zeros
Python Pad a String with Leading Zeros โ€“ Linux Hint
For padding a string with leading zeros in Python, the โ€œzfill()โ€ method can be used. It takes a number providing the required length of any Python string as an argument and adds zeros at the left side of the string until it is of the specified length.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ examples โ€บ display-numbers-with-leading-zeros
How to Display Numbers with Leading Zeros in Python? - AskPython
March 26, 2023 - To add leading zeros to numbers in Python, you can use the format() function, f-string technique, rjust() function, or zfill() function.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python int to string with leading zeros
Python Int to String with Leading Zeros - Be on the Right Side of Change
September 27, 2023 - To convert an integer i to a string with leading zeros so that it consists of 5 characters, use the format string f'{i:05d}'. The d flag in this expression defines that the result is a decimal value.