Search code examples
pythonunicodecodepoint

How to encode a long string of hex values in unicode easily in python


I have hex code point values for a long string. For a short one, following is fine.

msg = unichr(0x062A) + unichr(0x0627) + unichr(0x0628)
print msg

However, since unichr's alternate api unicode() does exist, i thought there must be a way to pass an entire code point string to it. So far i wasn't able to do it.

Now i have to type in a string of 150 hex values (code points) like the 3 above to generate a complete string. I was hoping to get something like

msg = unicode('0x062A, 0x0627....')

I have to use 'msg' latter. Printing it was a mere example. Any ideas?


Solution

  • Perhaps something like this:

    ", ".join(unichr(u) for u in (0x062A, 0x0627, 0x0628))
    

    Result:

    u'\u062a, \u0627, \u0628'
    

    Edit: This uses str.join.