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
🌐
Codecademy
codecademy.com › docs › python › strings › .zfill()
Python | Strings | .zfill() | Codecademy
November 19, 2023 - ... It creates a new string with leading zeros based on the specified integer argument, represented by total_length. The modified string is stored in the variable result. The following example shows how .zfill() returns a copy of the string ...
🌐
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....
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 - This article explains how to perform zero-padding on various data types such as strings (str) and integers (int) in Python. ... Specify the length of the returned string. The original string will be right-justified, and the left side will be filled with zeros. s = '1234' print(s.zfill(8)) # 00001234 print(type(s.zfill(8))) # <class 'str'>
🌐
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 ...
Find elsewhere
🌐
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().
🌐
Tutorialspoint
tutorialspoint.com › python › string_zfill.htm
Python String zfill() Method
In this example, we are creating three input strings: day, month and year, which does not follow the standard date format (DD/MM/YYYY) when concatenated together. So, we call the zfill() method on each of these strings, to add leading zeroes wherever necessary and obtain the date in the correct format.
🌐
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.
🌐
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.
🌐
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 Reference
python-reference.readthedocs.io › en › latest › docs › str › zfill.html
zfill — Python Reference (The Right Way) 0.1 documentation
>>> '350'.zfill(1) '350' >>> '350'.zfill(2) '350' >>> '350'.zfill(3) '350' >>> '350'.zfill(4) '0350' >>> '350'.zfill(10) '0000000350' >>> '350'.zfill(20) '00000000000000000350' >>> '-350'.zfill(5) '-0350'
🌐
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) ...
🌐
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.
🌐
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...