You are doing zfill on list object. Instead, you need to perform zfill on each item of list. Below is the sample example for range 10:
>>> a = range(0, 10)
# v this value represent the count of zeros
# v It should be `7` in your case
>>> [str(i).zfill(10) for i in a]
['0000000000', '0000000001', '0000000002', '0000000003', '0000000004', '0000000005', '0000000006', '0000000007', '0000000008', '0000000009']
As per the str.zfill() document:
Answer from Moinuddin Quadri on Stack Overflowstring.zfill(s, width)
Pad a numeric string s on the left with zero digits until the given width is reached. Strings starting with a sign are handled correctly.
You are doing zfill on list object. Instead, you need to perform zfill on each item of list. Below is the sample example for range 10:
>>> a = range(0, 10)
# v this value represent the count of zeros
# v It should be `7` in your case
>>> [str(i).zfill(10) for i in a]
['0000000000', '0000000001', '0000000002', '0000000003', '0000000004', '0000000005', '0000000006', '0000000007', '0000000008', '0000000009']
As per the str.zfill() document:
string.zfill(s, width)
Pad a numeric string s on the left with zero digits until the given width is reached. Strings starting with a sign are handled correctly.
you can also let str.format handle the filling:
for a in range(1000000):
print('{:07d}'.format(a))
Add a limit to `string.zfill` so it will raise an error to lengths bigger that the int in the brackets
Series.str.zfill() behaves differently than str.zfill() from standard library
Why won't zfill work on string objects in python? - Stack Overflow
How would you add zeros to the beginning of an Integer, such that there are 4 total digits in the number?
Does zfill change the original string?
Should I use zfill or an f-string?
How do I add leading zeros in Python?
Without using zfill or format or any other obscure functions.
example:
turn 15 into 0015
or 234 into 0234
I’m trying to learn all of Python’s built-in functions before starting OOP, so I’m curious how this function could be used in real projects.