Make use of the zfill() helper method to left-pad any string, integer or float with zeros; it's valid for both Python 2.x and Python 3.x.

It important to note that Python 2 is no longer supported.

Sample usage:

print(str(1).zfill(3))
# Expected output: 001

Description:

When applied to a value, zfill() returns a value left-padded with zeros when the length of the initial string value less than that of the applied width value, otherwise, the initial string value as is.

Syntax:

str(string).zfill(width)
# Where string represents a string, an integer or a float, and
# width, the desired length to left-pad.
Answer from nyedidikeke on Stack Overflow
🌐
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. It returns a copy of the string left filled with '0' digits to make a string of length width.
Discussions

python - How do I pad a string with zeros? - Stack Overflow
How do I pad a numeric string with zeroes to the left, so that the string has a specific length? More on stackoverflow.com
🌐 stackoverflow.com
How can I easily pad a float with x zeros on the left and round/pad to y decimal places on the right?
I think this should work: "%05.2f". It's padding to a minimum overall length of 5 (which includes the decimal point) and pads with 0 instead of spaces. More on reddit.com
🌐 r/godot
6
2
May 8, 2023
How to format numbers so numbers with leading zeros can be present?
Three of many possible base R solutions, sprintf("%04d", numbers) gettextf("%04d", numbers) formatC(numbers, width = 4, flag = "0") or you could roll your own, numbers <- substring(paste0(strrep("0", 4L), numbers), nchar(numbers) + 1L) sort(numbers) [1] "0123" "0124" "0125" "0126" "0127" "0128" "0129" "0134" "0135" "0136" [11] "0137" "0138" "0139" "0145" "0146" "0147" "0148" "0149" "0156" "0157" [21] "0158" "0159" "0167" "0168" "0169" "0178" "0179" "0189" "0234" "0235" [31] "0236" "0237" "0238" "0239" "0245" "0246" "0247" "0248" "0249" "0256" [41] "0257" "0258" "0259" "0267" "0268" "0269" "0278" "0279" "0289" "0345" [51] "0346" "0347" "0348" "0349" "0356" "0357" "0358" "0359" "0367" "0368" [61] "0369" "0378" "0379" "0389" "0456" "0457" "0458" "0459" "0467" "0468" [71] "0469" "0478" "0479" "0489" "0567" "0568" "0569" "0578" "0579" "0589" [81] "0678" "0679" "0689" "0789" "1234" "1235" "1236" "1237" "1238" "1239" [91] "1245" "1246" "1247" "1248" "1249" "1256" "1257" "1258" "1259" "1267" [101] "1268" "1269" "1278" "1279" "1289" "1345" "1346" "1347" "1348" "1349" [111] "1356" "1357" "1358" "1359" "1367" "1368" "1369" "1378" "1379" "1389" [121] "1456" "1457" "1458" "1459" "1467" "1468" "1469" "1478" "1479" "1489" [131] "1567" "1568" "1569" "1578" "1579" "1589" "1678" "1679" "1689" "1789" [141] "2345" "2346" "2347" "2348" "2349" "2356" "2357" "2358" "2359" "2367" [151] "2368" "2369" "2378" "2379" "2389" "2456" "2457" "2458" "2459" "2467" [161] "2468" "2469" "2478" "2479" "2489" "2567" "2568" "2569" "2578" "2579" [171] "2589" "2678" "2679" "2689" "2789" "3456" "3457" "3458" "3459" "3467" [181] "3468" "3469" "3478" "3479" "3489" "3567" "3568" "3569" "3578" "3579" [191] "3589" "3678" "3679" "3689" "3789" "4567" "4568" "4569" "4578" "4579" [201] "4589" "4678" "4679" "4689" "4789" "5678" "5679" "5689" "5789" "6789" All four of these give the same result. EDIT: For fun here's another, local({ x <- "0000" noquote(unname(sapply(as.character(numbers), \(y) {substring(x, 5L - nchar(y)) <- y; x}))) }) or if you're not on 4.1 yet, local({ x <- "0000" noquote(unname(sapply(as.character(numbers), function(y) {substring(x, 5L - nchar(y)) <- y; x}))) }) More on reddit.com
🌐 r/rstats
32
7
June 2, 2021
[deleted by user]
Regex generally isn't the right tool for this job. You can use Regex to capture the single digits, then whatever math/string function in your programming language to pad the number. But since we're always dealing with single-digit numbers, it's pretty simple. Also, did you post a question and say that you made some progress, but didn't post the Regex pattern that you came up with? Here's your answer: Regex101.com/r/HoA7YB/1 (26 steps) Regex pattern: _(\d)_ Substitution: _0\1_ A bit more complex: Regex101.com/r/HoA7YB/2 (117 steps) Regex pattern: (?<=_)(\d)(?=_) Substitution: 0\1 Even more complex: Regex101.com/r/HoA7YB/3 (131 steps) Regex pattern: (?<=_)(?=\d_) Substitution: 0 Complex isn't always better. In this case, additional complexity also increases the number of steps the Regex engine has to take when matching. Probably best to use #1 in this case. #2 and #3 both use positive lookbehinds and positive lookaheads to match but not consume characters (so we aren't replacing a character with itself). Let me know if you want me to explain further. More on reddit.com
🌐 r/regex
5
3
February 25, 2021
🌐
Linux Hint
linuxhint.com › python-pad-a-string-with-leading-zeros
Python Pad a String with Leading Zeros – Linux Hint
For padding a string with leading zeros in Python, the “zfill()” method can be used. It takes a number providing the required length of any Python string as an argument and adds zeros at the left side of the string until it is of the specified length.
🌐
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 - This method offers a straightforward ... number to a string using str(number) and then call the zfill() method to pad it with leading zeros until it has a width of 2 characters....
🌐
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.
🌐
iO Flood
ioflood.com › blog › python-zfill
Python's 'zfill()' Method | Guide To Padding Numeric Strings
February 14, 2024 - As you become more comfortable with Python’s zfill method, you’ll find it can handle a variety of scenarios beyond simple string padding. One such case is dealing with negative numbers.
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.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Pad Strings and Numbers with Zeros in Python (Zero-padding) | note.nkmk.me
May 18, 2023 - The original string will be ... the original string will be returned as it is. ... If a leading sign prefix (-, +) exists, the string is filled with zeros after the 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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-pad-a-string-to-a-fixed-length-with-zeros-in-python
How to Pad a String to a Fixed Length with Zeros in Python - GeeksforGeeks
July 23, 2025 - The simplest way to pad a string with zeros is by using Python’s built-in zfill() method. This method adds zeros to the left of a string until it reaches the desired length.
🌐
GeeksforGeeks
geeksforgeeks.org › python-add-leading-zeros-to-string
Add Leading Zeros to String - Python - GeeksforGeeks
January 17, 2025 - Explanation: zfill() pads the string with zeros to reach the specified width. Here, 4 + len(s) ensures the string s is padded with 4 zeros. In this method, we are multiplying the 0 as a string with the number of zeros required and using Python ...
🌐
Python Guides
pythonguides.com › pad-numbers-with-leading-zeros-in-python
How To Pad Numbers With Leading Zeros In Python?
March 19, 2025 - F-strings, introduced in Python 3.6, provide a concise and readable way to format strings. You can use f-strings to pad numbers with leading zeros by specifying the width and using the :0 format specifier.
🌐
Vultr Docs
docs.vultr.com › python › standard-library › str › zfill
Python str zfill() - Pad String with Zeros | Vultr Docs
December 31, 2024 - The str.zfill() method in Python is a straightforward approach for padding a string with zeros on the left side until it reaches a specified length.
🌐
Finxter
blog.finxter.com › home › learn python blog › python how to pad zeros to a string?
Python How to Pad Zeros to a String? - Be on the Right Side of Change
November 16, 2020 - 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.
🌐
Appdividend
appdividend.com › pad-a-string-with-zeros-in-python
How to Pad a String with Zeros in Python
September 7, 2025 - Zero-padding in Python: Efficiently format strings with leading zeros using zfill(), rjust(), f-strings, or format().
🌐
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 - By zero padding a number-like string allows you to safely reformat your data to be able to merge it across dataframes. The Python zfill method is used to create a copy of a string with zeros padding the string to a particular width.
🌐
Python Guides
pythonguides.com › pad-a-string-with-zeros-in-python
How To Pad A String With Zeros In Python?
January 29, 2025 - Another approach is to use Python’s string formatting capabilities. You can use the % operator or the format() method to pad strings with zeros.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python pad string with zeros
Python Pad String With Zeros - Spark By {Examples}
May 21, 2024 - How to pad string with zeros in Python? Adding some characters/values to the string is known as Padding. In Python, zfill() and rjust() methods are used