In Python 2 (and Python 3) you can do:

number = 1
print("%02d" % (number,))

Basically % is like printf or sprintf (see docs).


For Python 3.+, the same behavior can also be achieved with format:

number = 1
print("{:02d}".format(number))

For Python 3.6+ the same behavior can be achieved with f-strings:

number = 1
print(f"{number:02d}")
Answer from Jack M. on Stack Overflow
๐ŸŒ
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.
Discussions

Why does Python dump leading zeroes in integers?

An integer stores the value of an integer, and the value of an integer doesn't include leading zeroes. What you want is a specially formatted string representation of your integer like one of these:

>>> x = 33
>>> url = "http://url.com/"

>>> url + format(x, "06.0f")
'http://url.com/000033'

>>> "%s%06.0f" % (url, x)
'http://url.com/000033'

>>> "{}{:06.0f}".format(url, x)
'http://url.com/000033'
More on reddit.com
๐ŸŒ r/learnpython
6
5
June 3, 2012
Need zeros before the number in int
I have a string like โ€œ0001โ€ i am using int() and it returns 1 but i need 0001 and the type should be int, how can i do this? More on discuss.python.org
๐ŸŒ discuss.python.org
17
0
May 8, 2024
How to pad a string with leading zeros in Python 3 - Stack Overflow
I'm trying to make length = 001 in Python 3 but whenever I try to print it out it truncates the value without the leading zeros (length = 1). How would I stop this happening without having to cast ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 14, 2018
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
๐ŸŒ
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.
๐ŸŒ
Medium
medium.com โ€บ techmacademy โ€บ leading-zeros-in-python-a-hidden-mystery-revealed-ee3e3065634d
Leading Zeros in Python: A Hidden Mystery Revealed | by Estefania Cassingena Navone | Techmacademy | Medium
February 5, 2019 - The number must start with 0o or 0O (zero followed by an โ€œoโ€ letter in lowercase or uppercase). >>> 0o0000015 # Notice the "flag" 0o 13 >>> 0O0000015 # Notice the "flag" 0O 13 >>> 0000015 # We didn't add the "flag", so an error is thrown ...
๐ŸŒ
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 ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-add-leading-zeros-to-a-number-in-python
How to Add Leading Zeros to a Number in Python?
March 27, 2026 - Use zfill() for simple leading zero padding. For modern Python (3.6+), fstrings provide the cleanest syntax.
Find elsewhere
๐ŸŒ
Ashutosh Writes
blog.ashutoshkrris.in โ€บ how-to-display-numbers-with-leading-zeros-using-python
How to display numbers with leading zeros using Python?
March 18, 2023 - Being the oldest of the ways you can implement string formatting, it works without any problems on almost all the versions of Python that are currently available for use on the internet. The % sign and a letter representing the conversion type are marked as a placeholder for the variable. The following code uses the string formatting with the modulus (%) operator to display a number with leading zeros in Python.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-add-leading-zeros-to-string
Add Leading Zeros to String - Python - GeeksforGeeks
January 17, 2025 - 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) ... string.ljust(width, fillchar) method in Python pads the string with the specified fillchar (default is a space) on the left side until the string reaches the specified width.
๐ŸŒ
DataScience Made Simple
datasciencemadesimple.com โ€บ home โ€บ add leading zeros in python pandas (preceding zeros in data frame)
Add leading zeros in Python pandas (preceding zeros in data frame) - DataScience Made Simple
October 17, 2024 - zfill() function fills the character column with leading zeros. zfill() function with length of 10 fills the character column with preceding zeros till the total length reaches 10.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Need zeros before the number in int - Python Help - Discussions on Python.org
May 8, 2024 - I have a string like โ€œ0001โ€ i am using int() and it returns 1 but i need 0001 and the type should be int, how can i do this?
๐ŸŒ
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.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ add-leading-zeros-to-a-number-in-python
Add Leading Zeros to a Number in Python
July 2, 2022 - Strings in Python have a zfill method that can be used to pad a string with zeros to a certain length.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-print-leading-zeros-in-Python
How to print leading zeros in Python - Quora
Answer: By using the formatting specified - for example: [code]a = 73.5 print(fโ€™{a:0>10.2f}โ€™) [/code]Here : * 0 ie the fill character * > means right align the value (so the 0 will fill to the right) * 10 is the field width * 2 is the number of decimal digits * f is that the output is a fl...