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
🌐
Medium
medium.com › vicipyweb3 › mastering-leading-zero-magic-a-friendly-guide-to-formatting-leading-zeros-in-python-3cb934d7aa4f
Mastering Leading Zero Magic: A Friendly Guide to Formatting Leading Zeros in Python | by PythonistaSage | ViciPyWeb3 | Medium
May 3, 2023 - The f-string formats it with a minimum width of 2 characters, padding with a leading zero to get the result '07'. If you have a number with more digits than the specified width, it won’t be truncated or altered.
Discussions

Strange behavior: leading zeros in formatted print function are reversed
It's because m is the string '4' not the integer 4. The default for strings is left align, the default for integers is right align. You can either convert m and d to integers m = int(m) d = int(m) or you can specify the alignment direction explicitly print(f"{y}-{m:>02}-{d:>02}") More on reddit.com
🌐 r/learnpython
4
7
September 3, 2024
python - Best way to format integer as string with leading zeros? - Stack Overflow
If you need to store information ... with leading zeros until the hundreds place", integers alone cannot provide that - you need to use alternate data structures (string work well in this case) 2015-11-11T16:20:24.063Z+00:00 ... Need to say that this is not correct. The fact that 004 == 4 is kinda fortune. The way interpreter (python is not compiled) ... More on stackoverflow.com
🌐 stackoverflow.com
Help with f-string formatting please
Simplest way would be to add an if statement within your print. i.e. print(f"{i:+}" if i != 0 else i) More on reddit.com
🌐 r/learnpython
6
1
October 8, 2023
Suppress leading zeroes in date time formatting?
It's not the most elegant solution, but as far as I can tell the only time you should have " 0" will be when you have a leading 0 you want to remove so you could just tack on .replace(" 0", " ") More on reddit.com
🌐 r/learnpython
4
1
November 11, 2022
🌐
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.
🌐
YouTube
youtube.com › shorts › -PzkG1QTQqc
How To Add Leading Zeros In Python Using FStrings - YouTube
In this python tutorial, I show you how to add leadings zeros in python using f-strings. Let's get coding!======== Ask Case Digital ========If you have a que...
Published: February 24, 2025
🌐
Linux Hint
linuxhint.com › python-pad-a-string-with-leading-zeros
Python Pad a String with Leading Zeros – Linux Hint
Now, call the “print()” method with the “f-string” to pad the provided string with leading zeros. Here, the value “10” indicates that increase in the length of provided string by using zeros: ... As you can see, our provided strings have the “5” and “4” length, which are ...
🌐
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 - Example: For integer 42, you want to fill it up with leading zeros to the following string with 5 characters: '00042'. In all methods, we assume that the integer has less than 5 characters. The first method uses the format string feature in Python 3+ called f-strings or replacement fields.
Find elsewhere
🌐
Stack Abuse
stackabuse.com › bytes › add-leading-zeros-to-a-number-in-python
Add Leading Zeros to a Number in Python
July 2, 2022 - Note that if you're zero padding an integer (and not a string, like in the previous examples), you'll need to first convert the integer to a string: ... If you're using Python 3, you can use the f-string syntax to zero pad your strings.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-add-leading-zeros-to-a-number-in-python
How to Add leading Zeros to a Number in Python - GeeksforGeeks
July 23, 2025 - The string itself can be prepared ... approach that is not mentioned in the article is using the rjust() method of the built-in str class to add leading zeros to a number....
🌐
Plain English
python.plainenglish.io › get-better-at-python-f-strings-83b123bb4ce0
Get Better at Python f-strings. Make a coffee, sit back, and enjoy the… | by Görkem Arslan | Python in Plain English
July 10, 2021 - Indeed, this is a guide to improve your skills a little bit further while using f-strings. You can pad any number with zeros by using f-strings. A well-known example is to append leading zeros to ID numbers.
🌐
Fixpython
fixpython.com › python - common issues & solutions › fix f-string datetime leading zeros missing in python
Fix f-string datetime leading zeros missing in Python - FixPython
January 27, 2026 - Using the dash ("-") flag in a strftime format tells the C library to suppress padding, so month and day values less than 10 lose their leading zero. Python’s f-strings delegate to strftime, inheriting this platform‑dependent behavior. This follows the POSIX specification for the “-” flag.
🌐
TutorialsPoint
tutorialspoint.com › how-to-add-leading-zeros-to-a-number-in-python
How to Add Leading Zeros to a Number in Python?
May 31, 2023 - Fstrings provide the most modern and readable syntax for string formatting with leading zeros. number = 50 desired_width = 5 number_str = f"{number:0{desired_width}}" print("Number after leading zeros:", number_str) ...
🌐
Sololearn
sololearn.com › en › Discuss › 1564418 › how-to-use-alignment-and-leading-zeros-here-within-the-fstring
how to use alignment and leading zeros here (within the f-string)? | Sololearn: Learn to code for FREE!
Either you can fill up the f-string with spaces like {} {} - or you could use two f-strings and use the str justification methods, for example: print(f'{a:06}'.ljust(8), f'{b:06}'.ljust(8)) So this would mean that you rjust a number on a space ...
🌐
GeeksforGeeks
geeksforgeeks.org › python-add-leading-zeros-to-string
Add Leading Zeros to String - Python - GeeksforGeeks
January 17, 2025 - s = 'GFG' # Adding leading zeros res = '0' * 4 + s print("The string after adding leading zeros: " + res) ... We can directly add required no of zeros using python's string formatting.
🌐
Reddit
reddit.com › r/learnpython › strange behavior: leading zeros in formatted print function are reversed
r/learnpython on Reddit: Strange behavior: leading zeros in formatted print function are reversed
September 3, 2024 -

I'm currently learning Python through Harvard's free CS50 course, and while working on the Outdated assignment, I noticed something very bizarre in this specific part below:

while True:
    try:
        date = input("Date: ")
        if date.find("/"):
            m, d, y = date.split("/")
            print(f"{y}-{m:02}-{d:02}")
    except EOFError:
        print("")
        break

The leading zeros in the identifiers inside the print function result with zeros being in the end, as opposed to the left. For instance, if m is 4, in this case, m will result in 40, instead of 04. In another part of the script using a similar method, this does not occur. Is there something I'm doing wrong? Sorry if its a stupid question, I'm still learning.

🌐
Nono Martínez Alonso
nono.ma › leading-zeros-python
Leading zeros in Python · Nono Martínez Alonso
January 27, 2023 - Leading zeros are extra zeros to the left of a number when you want to have a regular amount of digits in a set of numbers. For instance, 0001, 0002, and 0003 is a good formatting if you think you'll get to have thousands of entries, as you ...
🌐
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 - On the other hand, the fillchar parameter is optional and represents the character that we want to pad the string with. Here, we have an example program where we need to display the numbers, 1 and 10, with leading zeros in each of the explained methods of this tutorial. The amount of padding required can be manually specified with the rjust() function. The following code uses the rjust() function to display a number with leading zeros in Python.
🌐
ZetCode
zetcode.com › python › fstring
Python f-string - formatting strings in Python with f-string
May 11, 2025 - You can pad numbers with leading zeros to ensure fixed-width output, or use commas as thousands separators to make large numbers easier to read. These features are especially helpful for reports, tables, or any output where alignment and clarity matter. ... #!/usr/bin/python number = 42 big_number ...
🌐
Python Engineer
python-engineer.com › posts › pad-zeros-string
How to pad zeros to a String in Python - Python Engineer
zfill is the best method to pad zeros from the left side as it can also handle a leading '+' or '-' sign. It returns a copy of the string left filled with '0' digits to make a string of length width.