novice3
novice3

Reputation: 606

How can I print the raw unicode in python?

I am novice in Python, so maybe I can't express it well...

I got a string '\xb9\xfe'

I want it print in this very fashion '\xb9\xfe', not converting to a Chinese character '哈'.

What is the proper way to do it?

Upvotes: 7

Views: 7544

Answers (2)

Manish Samuel
Manish Samuel

Reputation: 21

The correct answer is by using s.encode('unicode_escape').decode()

s = "asdf\u0642\u064f\u0644\u0652"
print(s.encode('unicode_escape').decode())

Output will be:

asdf\u0642\u064f\u0644\u0652

This will NOT work:

s = "asdf\u0642\u064f\u0644\u0652"
print(s)
print(repr(s))

Output will be:

asdfقُلْ
'asdfقُلْ'

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1123400

Use a raw string literal instead:

r'\xb9\xfe'

or print the output of repr() of your string:

print(repr('\xb9\xfe'))

Upvotes: 5

Related Questions