1

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?

fkl
  • 5,412
  • 4
  • 28
  • 68
  • 1
    If I understand this, you want to type `'0x062A, 0x0627....'` but balk at `'\u062a\u0627...'`. So type the first one then use the global replace function in your editor; that's what it's for. – Duncan Aug 08 '12 at 07:55

3 Answers3

2

Hard to tell what you're asking for exactly. Are you looking for u'\u062A\u0627\u0628'? The \u escape lets you enter individual characters by code point in a unicode string.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • yes i am looking for merely a combined string which i can latter say pass to a url with msg.encode('utf-16') etc. – fkl Aug 08 '12 at 07:42
  • @fayyazki: So are you saying the answer I gave is or is not what you are looking for? – BrenBarn Aug 08 '12 at 07:44
  • No it does the job. Only that i have to enter a \u for every code point value. Using string.join might seem easier (i am going to try it), since i can just copy paste the entire hex string in code and it would work. – fkl Aug 08 '12 at 07:50
2

Perhaps something like this:

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

Result:

u'\u062a, \u0627, \u0628'

Edit: This uses str.join.

jamylak
  • 128,818
  • 30
  • 231
  • 230
0

Following your example:

>>> c = u'\u062A\u0627\u0628'
>>> print c
تاب
mvillaress
  • 489
  • 3
  • 6
  • Thanks! The above though works and is one possible way, but i still have to enter a \u in front of every code point value. I am going to try with join since that seems to work with simply pasting entire hex string in the middle – fkl Aug 08 '12 at 07:48