there is no inbuilt zfill but I use this zfl function

def zfl(s, width):
# Pads the provided string with leading 0's to suit the specified 'chrs' length
# Force # characters, fill with leading 0's
return '{:0>{w}}'.format(s, w=width)

This might be useful to you? Just pass the string and the string width you require.

Answer from user12757608 on Stack Overflow
🌐
Programiz
programiz.com › python-programming › methods › string › zfill
Python String zfill()
Suppose, the initial length of the string is 10. And, the width is specified 15. In this case, zfill() returns a copy of the string with five '0' digits filled to the left.
🌐
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
Here, we are inputting a string, "this is string example....wow!!!", as the input and invoking the zfill() method on it twice, with different arguments '40' and '50'. The return value in each case will be the string with the leading zeroes upto given width.
🌐
W3Schools
w3schools.com › python › ref_string_zfill.asp
Python String zfill() Method
a = "hello" b = "welcome to the jungle" c = "10.000" print(a.zfill(10)) print(b.zfill(10)) print(c.zfill(10)) Try it Yourself »
🌐
DataScience Made Simple
datasciencemadesimple.com › home › zfill() function in python
zfill() Function in Python - DataScience Made Simple
September 22, 2020 - #zfill() for positive number string number1="345" print number1.zfill(4) # zfill() for negative number string number2="-675" print number2.zfill(6) ... In the below example we will be using zfill() function to pad the text string to desired length.
🌐
GeeksforGeeks
geeksforgeeks.org › python-string-zfill
Python String zfill() - GeeksforGeeks
January 2, 2025 - The zfill(3) method pads the string with two zeros on the left to make its total length 3.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Pad Strings and Numbers with Zeros in Python (Zero-padding) | note.nkmk.me
May 18, 2023 - s = '1234' print(s.zfill(8)) # 00001234 print(type(s.zfill(8))) # <class 'str'>
🌐
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 - Python zfill() method adds zeros at the beginning of a string until the desired length. For example "Hello".zfill(10) returns "00000Hello".
Find elsewhere
🌐
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 = 'Python programming' print('Original string:', txt) print('New string:', txt.zfill(25)) print('New string:', txt.zfill(20)) print('New string:', txt.zfill(18)) ... Original string: Python programming ...
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › hardware and peripherals › raspberry pi pico › micropython
Code from python to micropython problem - Raspberry Pi Forums
December 24, 2022 - In python it works normal, but in micropython an error is occcured... Any help would be apprecitable. Regards, Vlado ... import struct from ctypes import * def convert_int_to_bytes(x): y = x.to_bytes(4,"big",signed=False) z = [int(i) for i in y] return z def convert_float_to_MICROCHIP_32bit(x): x_IEEE = struct.pack(">f", x).hex() bits = bin(int(x_IEEE, 16))[2:].zfill(len(x_IEEE) * 4) i = 0 val = " ".encode() b_s = list(val) for char in bits: current_bit = int(char) if current_bit == 1: if (i > 0) and (i < 9): b_s[i - 1] = "1" elif i == 0: b_s[8] = "1" else: b_s[i] = "1" else: if (i > 0) and (i
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.char.zfill.html
numpy.char.zfill — NumPy v2.6.dev0 Manual
str.zfill · Examples · Try it in your browser! >>> import numpy as np >>> np.strings.zfill(['1', '-1', '+1'], 3) array(['001', '-01', '+01'], dtype='<U3') Go BackOpen In Tab ·
🌐
KooR.fr
koor.fr › Python › API › python › builtins › str › zfill.wp
KooR.fr - Méthode zfill - classe str - module builtins - Description de quelques librairies Python
__format__ __getnewargs__ __hash__ __init_subclass__ __iter__ __len__ __repr__ __sizeof__ __str__ __subclasshook__ capitalize casefold center count encode endswith expandtabs find format format_map index isalnum isalpha isascii isdecimal isdigit isidentifier islower isnumeric isprintable isspace istitle isupper join ljust lower lstrip maketrans partition removeprefix removesuffix replace rfind rindex rjust rpartition rsplit rstrip split splitlines startswith strip swapcase title translate upper zfill
Top answer
1 of 2
2

When you use

for i in t:

i is not index, each item.

>>> for i in t:
...     print(i)
...
2019
10
11
3
40
8
686538
None

If you want to use index, do like following:

>>> for i, v in enumerate(t):
...     print("{} is {}".format(i,v))
...
0 is 2019
1 is 10
2 is 11
3 is 3
4 is 40
5 is 8
6 is 686538
7 is None

another way to create '191011034008'

>>> t = (2019, 10, 11, 3, 40, 8, 686538, None)
>>> "".join(map(lambda x: "%02d" % x, t[:6]))
'20191011034008'
>>> "".join(map(lambda x: "%02d" % x, t[:6]))[2:]
'191011034008'

note that:

  1. %02d add leading zero when argument is lower than 10 otherwise (greater or equal 10) use itself. So year is still 4digit string.

  2. This lambda does not expect that argument is None.

I tested this code at https://micropython.org/unicorn/

edited :

str.format method version:

"".join(map(lambda x: "{:02d}".format(x), t[:6]))[2:]

or

"".join(map(lambda x: "{0:02d}".format(x), t[:6]))[2:]

second example's 0 is parameter index. You can use parameter index if you want to specify it (ex: position mismatch between format-string and params, want to write same parameter multiple times...and so on) .

>>> print("arg 0: {0}, arg 2: {2}, arg 1: {1}, arg 0 again: {0}".format(1, 11, 111))
arg 0: 1, arg 2: 111, arg 1: 11, arg 0 again: 1
2 of 2
1

I'd recommend you to use Python's string formatting syntax.

>> t = (2019, 10, 11, 3, 40, 8, 686538, None)
>> r = ("%d%02d%02d%02d%02d%02d" % t[:-2])[2:]
>> print(r)
191011034008

Let's see what's going on here:

  • %d means "display a number"
  • %2d means "display a number, at least 2 digits"
  • %02d means "display a number, at least 2 digits, pad with zeroes"

so we're feeding all the relevant numbers, padding them as needed, and cut the "20" out of "2019".

🌐
Codecademy
codecademy.com › docs › python › strings › .zfill()
Python | Strings | .zfill() | Codecademy
November 19, 2023 - ... It creates a new string with ... in the variable result. The following example shows how .zfill() returns a copy of the string with leading zeros....
🌐
YouTube
youtube.com › watch
zfill() String Method | Python Tutorial - YouTube
How to use the zfill() string method in Python to pad a string with leading zero characters. Source code: https://github.com/portfoliocourses/python-example-...
Published: February 6, 2023
🌐
Javatpoint
javatpoint.com › python-string-zfill-method
Python String | zfill() method with Examples - Javatpoint
We are excited to announce that we are moving from JavaTpoint.com to TpointTech.com on 10th Feb 2025. Stay tuned for an enhanced experience with the same great content and even more features. Thank you for your continued support · Python zfill() method fills the string at left with 0 digit ...
🌐
AskPython
askpython.com › python › string › python-string-zfill
Python string zfill() - AskPython
August 6, 2022 - Let’s look at some examples now. If there is no leading sign, the zeros are padded to the left. >>> a = "AskPython" >>> a.zfill(15) '00000AskPython'
🌐
Initial Commit
initialcommit.com › blog › python-zfill-method
Python zfill Method
November 1, 2021 - Here, we have the string orig with the length of 5. When we use the zfill() method on string orig with argument 10, the resulting value (stored in the variable padded) has zeros before it.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-add-leading-zeros-to-a-number-in-python
How to Add leading Zeros to a Number in Python - GeeksforGeeks
March 24, 2023 - The zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length.