ThePhi
ThePhi

Reputation: 2611

From the 4 numbers code-point to the unicode character?

I've got a 4 number string corresponding to the code-point of an unicode character. I need to dynamically convert it to its unicode character to be stored inside a variable.

For example, my program will spit during its loop a variable a = '0590'. (https://www.compart.com/en/unicode/U+0590)

How do I get the variable b = '\u0590'?

I've tried string concatenation '\u' + a but obviously it's not the way.

Upvotes: 0

Views: 84

Answers (1)

Silvio Mayolo
Silvio Mayolo

Reputation: 70277

chr will take a code point as an integer and convert it to the corresponding character. You need to have an integer though, of course.

a = '0590'
result = chr(int(a))
print(result)

On Python 2, the function is called unichr, not chr. And if you want to interpret the string as a hex number, you can pass an explicit radix to int.

a = '0590'
result = unichr(int(a, 16))
print(result)

Upvotes: 2

Related Questions