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
🌐
W3Schools
w3schools.com › python › ref_string_zfill.asp
Python String zfill() Method
Python Examples Python Compiler ... Q&A Python Training ... The zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length....
🌐
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.
People also ask

Should I use zfill or an f-string?
Use zfill for an existing string and sign-aware text padding; use an f-string when the source is numeric and the complete output format is known.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python zfill(): zero-pad strings, numbers, and signs
Python zfill(): Zero-Pad Strings, Numbers, and Signs
How do I add leading zeros in Python?
Convert the value to a string and call value.zfill(width), or use a numeric format such as f'{number:05d}' when formatting a number.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python zfill(): zero-pad strings, numbers, and signs
Python zfill(): Zero-Pad Strings, Numbers, and Signs
🌐
Note.nkmk.me
note.nkmk.me › home › python
Pad Strings and Numbers with Zeros in Python (Zero-padding) | note.nkmk.me
May 18, 2023 - To pad an integer with zeros, first convert it to a string using str(), then call zfill().
🌐
datagy
datagy.io › home › python posts › python strings › python zfill & rjust: pad a string in python
Python zfill & rjust: Pad a String in Python • datagy
December 16, 2022 - # Attempting to Pad an Integer with Zeroes number = 13 number.zfill(5) # Returns: AttributeError: 'int' object has no attribute 'zfill' We can see that when we attempt to apply the method to an integer, that an AttributeError is raised. In order to prevent this error, we need to first convert the integer into a string. We can do this using the str() function. Let’s see what this looks like: # Zero Padding an Integer in Python number = 13 print(str(number).zfill(7)) # Returns: 0000013
🌐
Initial Commit
initialcommit.com › blog › python-zfill-method
Python zfill Method
November 1, 2021 - Finally, have the character 'd' that signifies that we are using an integer for padding. But if we use the character 's' instead, strings can be used as well: ... We can see that the format() method is a much more powerful tool than zfill() and has more functionality. However, the Pythonic way of coding is simplicity.
🌐
Vultr Docs
docs.vultr.com › python › standard-library › str › zfill
Python str zfill() - Pad String with Zeros | Vultr Docs
December 31, 2024 - Convert each integer to a string. Pad each string with zeros to a specific length using str.zfill().
Find elsewhere
🌐
Python Pool
pythonpool.com › home › tutorials › python zfill(): zero-pad strings, numbers, and signs
Python zfill(): Zero-Pad Strings, Numbers, and Signs
July 13, 2026 - Use Python str.zfill() to add leading zeros, preserve plus and minus signs, and choose the right width for IDs and reports.
🌐
Plus2Net
plus2net.com › python › string-zfill.php
zfill() : fills left of the string with zeors by Python
my_str='plus2net' output=my_str.zfill(15) print(output) my_str='25' output=my_str.zfill(5) print(output) output is here ... If we are using any integer then we have to convert the same to string by using str() before using.
🌐
CodeRivers
coderivers.org › blog › zfill-python
Mastering `zfill` in Python: A Comprehensive Guide - CodeRivers
February 3, 2025 - Since zfill only accepts an integer as an argument, it's important to handle cases where the input might not be an integer.
🌐
iO Flood
ioflood.com › blog › python-zfill
Python's 'zfill()' Method | Guide To Padding Numeric Strings
February 14, 2024 - Using the zfill method, you can ensure these numeric elements have a consistent length, making your filenames easier to sort and manage. If you’re interested in diving deeper into Python’s string handling capabilities, there are a few related concepts you might want to explore.
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.14.7 documentation
Also referred to as integer division. For operands of type int, the result has type int. For operands of type float, the result has type float. In general, the result is a whole integer, though the result’s type is not necessarily int.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › str › zfill.html
zfill — Python Reference (The Right Way) 0.1 documentation
Returns the numeric string left filled with zeros in a string of specified length · A sign prefix is handled correctly. The original string is returned if width is less than or equal to len(str)
🌐
Tutorialspoint
tutorialspoint.com › python › string_zfill.htm
Python String zfill() Method
The Python String zfill() method does not fill the string if the length of the string is greater than the total width of the string after padding.
🌐
Stack Abuse
stackabuse.com › bytes › add-leading-zeros-to-a-number-in-python
Add Leading Zeros to a Number in Python
July 2, 2022 - The parameter to zfill is the length of the string you want to pad, not necessarily the number of zeros to add. So in the example above, if your string is already 5 characters long, it will not add any zeros. ... No spam ever. Unsubscribe anytime. Read our Privacy Policy.
🌐
Runebook.dev
runebook.dev › en › docs › python › library › stdtypes › str.zfill
python - The str.zfill() Method: Best Practices, Common Errors, and the f-String Way
Since zfill() is a string method, you'll get an AttributeError if you try to call it directly on a number type (int or float). ... Always convert the number to a string before using zfill(). num = 42 padded_num = str(num).zfill(5) print(padded_num) ...
🌐
docs.python.org
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.13.5 documentation
The following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions. Some colle...