You can use the zfill() method to pad a string with zeros:

In [3]: str(1).zfill(2)
Out[3]: '01'
Answer from unbeknown 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 - So, in this example, the number variable has the value of 7, pywhich is a single-digit number. The f-string formats it with a minimum width of 2 characters, padding with a leading zero to get the result '07'.
🌐
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.

Discussions

How to pad a string with leading zeros in Python 3 - Stack Overflow
I'm trying to make length = 001 ... the leading zeros (length = 1). How would I stop this happening without having to cast length to a string before printing it out? ... It depends how you've got 001. length = 001 is the same as length = 1' (number) and to get 001` you should use some string formatting (see ... More on stackoverflow.com
🌐 stackoverflow.com
f-strings in Python 3.6 are awesome
I feel like you should have given some examples. For us who are not familiar with f-strings this post doesn't really tell us much. More on reddit.com
🌐 r/Python
233
253
March 31, 2017
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
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
🌐
Towards Data Science
towardsdatascience.com › home › latest › 4 tricks to use python f-strings more efficiently
4 Tricks to Use Python F-strings More Efficiently | Towards Data Science
January 17, 2025 - "b:03" indicates that there will be a total of 3 digits and leading spaces will be padded with zero. In case of a one-digit number, we have 2 leading zeros. If it was "b:04", the number would be written as 0004 and 0123.
🌐
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
🌐
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.
🌐
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 - Use zfill() for simple leading zero padding. For modern Python (3.6+), fstrings provide the cleanest syntax.
Find elsewhere
🌐
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 ...
🌐
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.
🌐
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.
🌐
Stack Abuse
stackabuse.com › bytes › add-leading-zeros-to-a-number-in-python
Add Leading Zeros to a Number in Python
July 2, 2022 - 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 similarly to how str.format would be used (). F-strings offer a clear and practical method for formatting python expressions inside of string literals. ... One 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.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Pad Strings and Numbers with Zeros in Python (Zero-padding) | note.nkmk.me
May 18, 2023 - i = 255 print('Zero Padding: {:08}'.format(i)) # Zero Padding: 00000255 print('Zero Padding: {:08x}'.format(i)) # Zero Padding: 000000ff i = -1234 print('Zero Padding: {:08}'.format(i)) # Zero Padding: -0001234 ... In Python 3.6 or later, you can also use f-strings for a more concise representation. ... You can use the % operator with a string to achieve formatted output. Note that f-strings and format() are recommended in the official documentation. Note: The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly).
🌐
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.
🌐
ZetCode
zetcode.com › python › fstring
Python f-string - formatting strings in Python with f-string
May 11, 2025 - F-strings provide powerful options for formatting numbers. 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.
🌐
Miguendes
miguendes.me › 73-examples-to-help-you-master-pythons-f-strings
Python F-String: 73 Examples to Help You Master It
July 2, 2022 - >>> import datetime >>> now = ... '2020-10-23 20:24:17' You can add leading zeros by adding using the format {expr:0len} where len is the length of the returned string....
🌐
GeeksforGeeks
geeksforgeeks.org › python-add-leading-zeros-to-string
Add Leading Zeros to String - Python - GeeksforGeeks
January 17, 2025 - The width is calculated as 4 + len(s), ensuring that four zeros are added before the original string. In this approach, we are using a while loop to manually append the required number of zeros to the string s. The loop runs N times, adding one zero per iteration, and then the original string s is concatenated to the result. ... s = "GFG" N = 4 # Add leading zeros using a while loop res = "" i = 0 while i < N: res += "0" i += 1 res += s print("The string after adding leading zeros: " + res)
🌐
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 - If the value specified of this width parameter is less than the length of the string, the filling process does not occur. The following code uses the str.zfill() function to display a number with leading zeros in Python.