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?
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?
Use a raw string literal instead:
r'\xb9\xfe'
or print the output of repr()
of your string:
print(repr('\xb9\xfe'))
'asdfقُلْ'
>>> repr(a) "'asdfقُلْ'"
–
Kalpa ascii()
instead of repr()
if you want non-ASCII printable characters to be represented as escape sequences. –
Committeeman 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قُلْ'
© 2022 - 2024 — McMap. All rights reserved.