Search code examples
pythonunicode

Question about unicode assignments in python


I have been learning Python for about a year. I need to understand using unicode glyphs. In particular, this set: https://unicode.org/charts/PDF/U11D60.pdf

I tried this and when I tried to print it, (O didn't get the expected glyph:

a = '\u11D72'
print(a)

I'm using VSCode on a Mac. What am I missing? I want to use these glyphs for an encryption program project using a translate table.

Something like this:

mystr = 'String to be translated'
trans_dict = {'A':'\u11D72', 'B':'\u11D73', 'C':'\u11D74', ...}
mytable = mystr.maketrans(trans_dict)
mystr_translated = mystr.translate(mytable)

Solution

  • You have to use a \U + 8-hexadecimal code instead of \u + 4-hexadecimal code when the code point is more than four digits. You'll also need an appropriate font so it may or may not show up with the correct glyph on your system.

    import unicodedata as ud
    
    mytable = mystr.maketrans('ABC', '\U00011D72\U00011D73\U00011D74')
    mystr_translated = 'ABC'.translate(mytable)
    for c in mystr_translated:
        print(c, ud.name(c))
    

    Output:

    𑵲 GUNJALA GONDI LETTER KHA
    𑵳 GUNJALA GONDI LETTER TA
    𑵴 GUNJALA GONDI LETTER THA
    

    See Unicode HOWTO: Unicode Literals in Python Source Code