[Tutor] unicode question

Alan Gauld alan.gauld at yahoo.co.uk
Mon Jun 6 18:22:57 EDT 2022


On 06/06/2022 21:04, Alex Kleider wrote:

>>>> middle_dot = '0140', '013F', '00B7', '2027'
>>>> ucode = ['\\u' + dot for dot in middle_dot]

This creates a list of literal strings like

"\u0140",....

Which is identical to writing:

ucode = ["\\u0140",...]

So you have the \u as characters within the string
which is not what you want.

Instead you want to get the character values from
the hex values in middle dot which you can do using
chr() and int().

for dot in middle_dot:
   print(chr(int(dot,16)))

However it would be easier just to store the hex values directly:

middle_dot = [0x0140, 0x013F, 0x00B7, 0x2027)
for dot in middle_dot:
    print(chr(dot))

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list