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
🌐
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)
🌐
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
🌐
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'>
🌐
W3Schools
w3schools.com › python › ref_string_zfill.asp
Python String zfill() Method
The zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified 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.
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".

Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › python › string_zfill.htm
Python String zfill() Method
Note: The zfill() method works similar to the rjust() method if we assign '0' to the fillchar parameter of the rjust() method.
🌐
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
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.char.zfill.html
numpy.char.zfill — NumPy v2.6.dev0 Manual
Return the numeric string left-filled with zeros. A leading sign prefix (+/-) is handled by inserting the padding after the sign character rather than before · Width of string to left-fill elements in a
🌐
Reddit
reddit.com › r/learnpython › [micropython] convert a string to binary 1s and 0s.
r/learnpython on Reddit: [Micropython] Convert a string to binary 1s and 0s.
January 13, 2022 -

Let's say I input a as my variable. I want it to display 01100001

That's it. I've tried everything but for some reason micropython shits itself when I try to use conversion_binary = ''.join(format(ord(i), '08b') for i in lett)"

And yet normal Python works just fine. I don't get it. It just says that "format" isn't defined.

🌐
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
🌐
DataScience Made Simple
datasciencemadesimple.com › home › zfill() function in python
zfill() Function in Python - DataScience Made Simple
September 22, 2020 - zfill() function takes up the string and fills the string with preceding zeros until the desired length is obtained.
🌐
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 - If the initial length of the string is 10, and the width parameter specified is 5, then the zfill() function does not fill ‘0’ digits to the left, instead, it returns the exact same copy of the original string.
🌐
LabEx
labex.io › tutorials › python-how-to-pad-binary-string-with-zeros-462156
How to pad binary string with zeros | LabEx
def pad_binary_left(binary_str: str, length: int = 8) -> str: """ Left pad binary string with zeros Args: binary_str: Input binary string length: Desired total length Returns: Padded binary string """ return binary_str.zfill(length) def pad_binary_right(binary_str: str, length: int = 8) -> str: """ Right pad binary string with zeros Args: binary_str: Input binary string length: Desired total length Returns: Padded binary string """ return binary_str.ljust(length, '0')
🌐
AskPython
askpython.com › python › string › python-string-zfill
Python string zfill() - AskPython
August 6, 2022 - The Python string zfill() method is used to pad a string with zeros on the left until a specific width is reached. This is the most "Pythonic" way to add
🌐
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 - If a string starts with the prefix of + or -, the zfill() method adds the zeros after the first occurrence of the prefix.
🌐
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.
🌐
Initial Commit
initialcommit.com › blog › python-zfill-method
Python zfill Method
November 1, 2021 - We apply the zfill() method to a string value while passing a single argument, which must be an integer. It returns the zero-padded version of the original string. The argument it takes is the length of the string that should be returned after ...