Yes, there are the string justification methods, ljust and rjust.

>>> '12'.rjust(5, '#')
'###12'
>>> 'txt'.rjust(5, ' ')
'  txt'
>>> '12'.ljust(5, '#')
'12###'
Answer from PM 2Ring on Stack Overflow
🌐
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 - These alternatives included the rjust method and Python string formatting. To learn more about the Python str.zfill() method, check out the official documentation here.
Discussions

Arcade equivalent of Python ".zfill" - Geographic Information Systems Stack Exchange
What is the equivalent of the "zfill" python function to set a specific number of digits for values in a field using Arcade? More on gis.stackexchange.com
🌐 gis.stackexchange.com
February 21, 2023
Equivalent of Python zfill? - Ruby - Ruby-Forum
Hello all, I need a function which takes a number (x) and outputs a string which contains x zeroes. e.g. foo(3) = β€œ000” foo(5) = β€œ00000” etc. in Python this function is β€˜β€™.zfill(5) another possibility would be 5*β€˜0’ Equivalents in Ruby? I would be mainly interested in a zfill ... More on ruby-forum.com
🌐 ruby-forum.com
1
0
May 29, 2006
How to pad a numeric string with zeros to the right in Python? - Stack Overflow
Python strings have a method called zfill that allows to pad a numeric string with zeros to the left. ... You could always convert the string to an integer, multiply by 10**(num-length), and convert back to string. ... Save this answer. ... Show activity on this post. As maybe a alternative more ... More on stackoverflow.com
🌐 stackoverflow.com
python - How do I pad a string with zeros? - Stack Overflow
Rather, the Python documentation website behind that link was rewritten. Apart from that, the documentation used to have stronger wording, and literally states that str.format β€œshould be preferred to the % formatting”, just as I wrote in the comment you quoted. 2019-06-11T23:25:50.077Z+00:00 ... Save this answer. ... Show activity on this post. Copy>>> '99'.zfill... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Note.nkmk.me
note.nkmk.me β€Ί home β€Ί python
Pad Strings and Numbers with Zeros in Python (Zero-padding) | note.nkmk.me
May 18, 2023 - Using the newer formatted string literals, the str.format() interface, or template strings may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility. Built-in Types - printf-style String Formatting β€” Python 3.11.3 documentation
🌐
Ruby-Forum
ruby-forum.com β€Ί t β€Ί equivalent-of-python-zfill β€Ί 61886
Equivalent of Python zfill? - Ruby - Ruby-Forum
May 29, 2006 - Hello all, I need a function which takes a number (x) and outputs a string which contains x zeroes. e.g. foo(3) = β€œ000” foo(5) = β€œ00000” etc. in Python this function is β€˜β€™.zfill(5) another possibility would be 5*β€˜β€¦
🌐
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
Here is a friendly guide covering common gotchas and an alternative method using the more classic approach.The hmac. digest() function is a helper function introduced in Python 3.7.
🌐
iO Flood
ioflood.com β€Ί blog β€Ί python-zfill
Python's 'zfill()' Method | Guide To Padding Numeric Strings
February 14, 2024 - Along the way, we tackled common ... comparing Python’s zfill method with other string padding techniques, such as the format method and custom functions....
🌐
Initial Commit
initialcommit.com β€Ί blog β€Ί python-zfill-method
Python zfill Method
November 1, 2021 - 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. So oftentimes it's better to use a simpler method that does the specific task than ...
Find elsewhere
🌐
Codingem
codingem.com β€Ί home β€Ί python string zfill() method: a complete guide (with examples)
Python String zfill() Method: A Complete Guide (with Examples)
November 1, 2022 - This method can be used to do the same as the zfill() method. ... Python’s built-in zfill() method pads a string by adding a number of zeros at the beginning of the string.
🌐
Toppr
toppr.com β€Ί guides β€Ί python-guide β€Ί references β€Ί methods-and-functions β€Ί methods β€Ί string β€Ί zfill β€Ί python-string-zfill
Python zfill() function | Why do we use Python String zfill() function? |
September 27, 2021 - # Python program to illustrate zfill() txt = '+1234' print('Original string:', txt) print('New string:', txt.zfill(8)) txt = '-50' print('Original string:', txt) print('New string:', txt.zfill(5)) txt = '++hello-world+' print('Original string:', txt) print('New string:', txt.zfill(18)) ... Original string: +1234 New string: +0001234 Original string: -50 New string: -0050 Original string: ++hello-world+ New string: +0000+hello-world+ There are alternate functions that Python provides in order to carry out the similar tasks which Python zfill() does.
🌐
AskPython
askpython.com β€Ί python β€Ί string β€Ί python-string-zfill
Python string zfill() - AskPython
August 6, 2022 - Similar to str.zfill(), the str.rjust() can also give padding to a string, but also allows us to specify the character to pad the string with.
🌐
Runebook.dev
runebook.dev β€Ί en β€Ί docs β€Ί python β€Ί library β€Ί stdtypes β€Ί bytearray.zfill
python - Zero-Padding ByteArrays: Tips, Traps, and Alternatives to zfill()
bytearray.zfill() is fantastic for quickly padding byte-based numeric or simple ASCII data. Just keep these points in mind Β· Use it on bytearray objects. It pads with b'0' (ASCII '0'). ... Here's a friendly and detailed breakdown of common issues and alternative methods when working with Python's smtplib.The smtplib
🌐
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. 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.
🌐
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 - Let's explore various other methods we can use to pad a string to a fixed length with zeroes in python are: ... The rjust() method is another efficient option for padding strings.
🌐
Better Stack
betterstack.com β€Ί community β€Ί questions β€Ί how-to-pad-string-with-zeroes-in-python
How do I pad a string with zeroes in Python? | Better Stack Community
February 3, 2023 - To get a substring of a string in Python, you can use the string slicing notation, which is string[start:end], where start is the index of the first character of the substring, and end is the index...
Top answer
1 of 1
3

Edit: Polars 0.13.43 and later

With version 0.13.43 and later, Polars has a str.zfill expression to accomplish this. str.zfill will be faster than the answer below and thus str.zfill should be preferred.


From your question, I'm assuming that you are starting with a column of integers.

lambda x: str(x).zfill(5)

If so, here's one that adheres to pandas rather strictly:

import polars as pl
df = pl.DataFrame({"num": [-10, -1, 0, 1, 10, 100, 1000, 10000, 100000, 1000000, None]})

z = 5
df.with_columns(
    pl.when(pl.col("num").cast(pl.String).str.len_chars() > z)
    .then(pl.col("num").cast(pl.String))
    .otherwise(pl.concat_str(pl.lit("0" * z), pl.col("num").cast(pl.String)).str.slice(-z))
    .alias("result")
)
shape: (11, 2)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ num     ┆ result  β”‚
β”‚ ---     ┆ ---     β”‚
β”‚ i64     ┆ str     β”‚
β•žβ•β•β•β•β•β•β•β•β•β•ͺ═════════║
β”‚ -10     ┆ 00-10   β”‚
β”‚ -1      ┆ 000-1   β”‚
β”‚ 0       ┆ 00000   β”‚
β”‚ 1       ┆ 00001   β”‚
β”‚ 10      ┆ 00010   β”‚
β”‚ …       ┆ …       β”‚
β”‚ 1000    ┆ 01000   β”‚
β”‚ 10000   ┆ 10000   β”‚
β”‚ 100000  ┆ 100000  β”‚
β”‚ 1000000 ┆ 1000000 β”‚
β”‚ null    ┆ null    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Comparing the output to pandas:

df.with_columns(pl.col('num').cast(pl.String)).get_column('num').to_pandas().str.zfill(z)
0       00-10
1       000-1
2       00000
3       00001
4       00010
5       00100
6       01000
7       10000
8      100000
9     1000000
10       None
dtype: object

If you are starting with strings, then you can simplify the code by getting rid any calls to cast.

Edit: On a dataset with 550 million records, this took about 50 seconds on my machine. (Note: this runs single-threaded)

Edit2: To shave off some time, you can use the following:

result = df.lazy().with_columns(
    pl.col('num').cast(pl.String).alias('tmp')
).with_columns(
    pl.when(pl.col("tmp").str.len_chars() > z)
    .then(pl.col("tmp"))
    .otherwise(pl.concat_str(pl.lit("0" * z), pl.col("tmp")).str.slice(-z))
    .alias("result")
).drop('tmp').collect()

but it didn't save that much time.