Python Forum

Full Version: Zfill Method Parameter Confusion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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'
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}'
'🎉'
Thank you both!