tastyminerals
tastyminerals

Reputation: 6548

How do I convert unicode code to string in Python?

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

Answers (3)

falsetru
falsetru

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

aIKid
aIKid

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

Chris Seymour
Chris Seymour

Reputation: 85873

>>> print u"\u0631\u064e\u062c\u0627"
رَجا

Upvotes: 1

Related Questions