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
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-pandas-int-to-string-with-leading-zeros
Python | pandas int to string with leading zeros - GeeksforGeeks
July 23, 2025 - You can use pandas Series .map() and Python's string formatting to convert integers to strings with the leading zeros.
๐ŸŒ
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. The rjust() method returns a right-justified string, with optional padding, based on a specified width.
๐ŸŒ
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?
๐ŸŒ
Quora
quora.com โ€บ How-do-you-print-an-integer-with-leading-zeros-in-Python
How to print an integer with leading zeros in Python - Quora
Answer (1 of 3): You can use the Python zfill(n) function which facilitates the printing of an integer with leading zeroes. However, the integer must be converted to a string, and the total number of characters printed will be up to n, but not ...
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ strings โ€บ .zfill()
Python | Strings | .zfill() | Codecademy
November 19, 2023 - Note: The .zfill() method does not change the string it is used on. ... Learn to analyze and visualize data using Python and statistics. ... Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages ...
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-add-leading-zeros-to-number
How to add leading Zeros to a Number in Python | bobbyhadz
Alternatively, you can use a formatted string literal to add leading zeros to a number. ... Copied!num = 13 result_1 = f'{num:04}' print(result_1) # ๐Ÿ‘‰๏ธ '0013' result_2 = f'{num:05}' print(result_2) # ๐Ÿ‘‰๏ธ '00013' ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-add-leading-zeros-to-a-number-in-python
How to Add Leading Zeros to a Number in Python?
Python is a versatile programming ... the task. One method to add leading zeros to a number is to convert the number to a string first and then use the zfill() method to add the leading necessary zeros....
๐ŸŒ
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. This is done using the extra formatting syntax that specifies the number of zeros to add. ... Here the 03 specifies that the string should be padded with 3 zeros. The second digit in this argument is equivalent ...
๐ŸŒ
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 - Introduced in Python 3.6, f-strings have become a popular choice among Python developers for their simplicity and efficiency. ... In this example, we use an f-string with a :02 format specifier. In the f-string example, the :02 format specifier ...
๐ŸŒ
Folkstalk
folkstalk.com โ€บ home โ€บ 2022 โ€บ october
Python Convert Number To String With Leading Zeros With Code Examples
October 4, 2022 - ... Use the str() class to convert the number to a string. Use the str. zfill() method to add leading zeros to the string. The method takes the width of the string and pads it with leading zeros.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_zfill.asp
Python String zfill() Method
Remove List Duplicates Reverse ... Python Certificate Python Training ... The zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length....
๐ŸŒ
W3docs
w3docs.com โ€บ python
Display number with leading zeros
For example, to display the number 5 with leading zeroes to make it 2 digits wide, you can use the following code: number = 5 print("{:02d}".format(number)) Try it Yourself ยป ยท Copy ยท This will output 05. You can also use f-strings (Python 3.6+) number = 5 print(f"{number:02d}") Try it Yourself ยป ยท Copy ยท This will also output 05. Tags ยท integer python string-formatting ยท
๐ŸŒ
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 - 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.