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?
Answers:
Use a raw string literal instead:
r'xb9xfe'
or print the output of repr()
of your string:
print(repr('xb9xfe'))
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قُلْ'
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?
Use a raw string literal instead:
r'xb9xfe'
or print the output of repr()
of your string:
print(repr('xb9xfe'))
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قُلْ'