How can I print the raw unicode in python?

Question:

I am novice in Python, so maybe I can’t express it well…

I got a string 'xb9xfe'

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

What is the proper way to do it?

Asked By: novice3

||

Answers:

Use a raw string literal instead:

r'xb9xfe'

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

print(repr('xb9xfe'))
Answered By: Martijn Pieters

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

s = "asdfu0642u064fu0644u0652"
print(s.encode('unicode_escape').decode())

Output will be:

asdfu0642u064fu0644u0652

This will NOT work:

s = "asdfu0642u064fu0644u0652"
print(s)
print(repr(s))

Output will be:

asdfقُلْ
'asdfقُلْ'
Answered By: Manish Samuel
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.