Reputation: 6548
One of my string variables contains unicode code \u0631\u064e\u062c\u0627
.
I want to convert it to a string and see what characters were encoded by it.
Is it possible and how can I do it in python?
Upvotes: 4
Views: 679
Reputation: 369424
Decode using unicode_escape
encoding:
In Python 2.x:
>>> text = r'\u0631\u064e\u062c\u0627'
>>> print(text)
\u0631\u064e\u062c\u0627
>>> print(text.decode('unicode-escape'))
رَجا
In Python 3.x:
>>> text = r'\u0631\u064e\u062c\u0627'
>>> print(text.encode().decode('unicode-escape'))
رَجا
Upvotes: 3
Reputation: 28312
That's just an internal representation. If you print it, you will get what you want:
>>> print("\u0631\u064e\u062c\u0627")
رَجا
In short, this is how Python stores the characters رَ
and جا
. If you tell Python to print them, you'll see them converted back to their human-readable form.
Upvotes: 3