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
๐ŸŒ
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.
Discussions

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
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
python - Display number with leading zeros - Stack Overflow
Basically zfill takes the number of leading zeros you want to add, so it's easy to take the biggest number, turn it into a string and get the length, like this: Python 3.6.5 (default, May 11 2018, 04:00:52) [GCC 8.1.0] on linux Type "help", "copyright", "credits" or "license" for more information. More on stackoverflow.com
๐ŸŒ stackoverflow.com
In Python, how to specify a format when converting int to string? - Stack Overflow
In Python, how do I specify a format when converting int to string? More precisely, I want my format to add leading zeros to have a string with constant length. For example, if the constant lengt... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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 less than the number of digits in the given integer. ...
๐ŸŒ
pythontutorials
pythontutorials.net โ€บ blog โ€บ in-python-how-to-specify-a-format-when-converting-int-to-string
How to Convert Int to String with Leading Zeros for Fixed Length in Python โ€” pythontutorials.net
It works directly on string representations of integers and handles negative numbers gracefully. ... Convert the integer to a string first (e.g., str(5) โ†’ "5"). zfill(width) then adds leading zeros until the string length equals width.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ strings โ€บ .zfill()
Python | Strings | .zfill() | Codecademy
November 19, 2023 - Returns a string with zeros padding the left side based on the integer given.
๐ŸŒ
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 rjust() method returns a right-justified string, with optional padding, based on a specified width. Here is an example of how you could use the rjust() method to add leading zeros to a number: ... Using the rjust() method can be a simple ...
๐ŸŒ
Python Guides
pythonguides.com โ€บ convert-int-to-string-in-python
Convert Integer to String in Python (5 Easy Ways)
3 weeks ago - Convert an integer to a string in Python with str(), f-strings and format(), plus leading zeros, commas, hex, lists of ints and the 4,300-digit limit.
๐ŸŒ
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?
๐ŸŒ
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 str.zfill() method is a string method that pads a numeric string with leading zeros until it reaches the specified width. This method offers a straightforward way to handle leading zeros in Python.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ examples โ€บ display-numbers-with-leading-zeros
How to Display Numbers with Leading Zeros in Python? - AskPython
March 26, 2023 - ... This method uses a combination of the str( ) function along with the rjust( ) function to add leading zeros to any given number. The rjust( ) function would normally be used to return a string which is right justified.
๐ŸŒ
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 - number = 50 desired_width = 5 # Convert number to string and use zfill method number_str = str(number).zfill(desired_width) print("Number after leading zeros:", number_str) ... The rjust() method rightjustifies a string and fills the remaining space with a specified character โ€” in this case, zeros. number = 50 desired_width = 5 number_str = str(number).rjust(desired_width, "0") print("Number after leading zeros:", number_str) ... Python's string formatting syntax allows specifying width and fill characters using the format() method.
๐ŸŒ
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.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ python-basic-exercise-140.php
Python: Convert an integer to binary keep leading zeros - w3resource
May 17, 2025 - # The 'format' function is used with the format specifier '08b' to format 'x' as an 8-character binary string. # It ensures that there are leading zeros to make it 8 characters long.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-add-leading-zeros-to-number
How to add leading Zeros to a Number in Python | bobbyhadz
Use the str() class to convert the number to a string. Use the str.zfill() method to add leading zeros to the string.
๐ŸŒ
Linuxize
linuxize.com โ€บ home โ€บ python โ€บ how to convert integer into string in python
How to Convert Integer into String in Python | Linuxize
1 month ago - Convert an int to a str in Python with str(), f-strings, and format(), add leading zeros, build binary or hex strings, and join a list of numbers.