Python Forum
Zfill Method Parameter Confusion - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Zfill Method Parameter Confusion (/thread-38796.html)



Zfill Method Parameter Confusion - new_coder_231013 - Nov-25-2022

Hello,

I was going through a tutorial on Unicode and character encodings in Python and I came across a function that the author presents as a way to convert Unicode strings that look like "U+10346" into actual Unicode characters. The function is as follows:

def make_uchr(code: str):
     return chr(int(code.lstrip("U+").zfill(8), 16))
I'm having trouble understanding why there are two numbers as parameters after ".zfill." Everything I read online says that .zfill only takes one parameter (the specified length of the zero-filled string that is sought) and I don't believe the "16" is an argument that is used by the lstrip method. If anyone could help clarify this for me I'd greatly appreciate it. Thank you.


RE: Zfill Method Parameter Confusion - Gribouillis - Nov-25-2022

16 is a second argument to int(). It is assumed that the string contains hexadecimal characters and it is converted to int in base 16. For example
>>> int("000002A3", 16)
675
>>> chr(int("000002A3", 16))
'ĘŁ'
>>> '\N{LATIN SMALL LETTER DZ DIGRAPH}'
'ĘŁ'
for the character 'LATIN SMALL LETTER DZ DIGRAPH'


RE: Zfill Method Parameter Confusion - snippsat - Nov-26-2022

Can drop filling in zeros and it will still work.
>>> chr(int(u.lstrip("U+"), 16))
'𐍆'

>>> u = 'U+1F496'
>>> chr(int(u.lstrip("U+"), 16))
'💖'

>>> u = 'U+1F47D'
>>> chr(int(u.lstrip("U+"), 16))
'👽'

>>> u = "U+1f4af"
>>> chr(int(u.lstrip("U+"), 16))
'💯'

>>> u = 'U+1F389'
>>> chr(int(u.lstrip("U+"), 16))
'🎉'

# Using name
>>> '\N{Party Popper}'
'🎉'



RE: Zfill Method Parameter Confusion - new_coder_231013 - Dec-05-2022

Thank you both!