translating Unicode characters from input

Question:

I have a string with unicode characters that I need to decode. When I hardcode the string into python it seems to work. However, if I get it through an input, it doesn’t translate. For example,

input_0 = input() #fu00eate
print(input_0) # prints fu00eate
word = "fu00eate"
print(word) # prints fĂȘte

How could I turn the Unicode parts of the string from the input into regular characters? I have tried using str(word) too.

Asked By: Cobalt-monster

||

Answers:

What you get from input() is a raw-string which means you don’t have escape sequence they are literal characters. u00ea is 6 characters.

You should encode it with "raw-unicode-escape" and then decode it with "unicode-escape":

input_0 = input()  # fu00eate
print(input_0.encode("raw-unicode-escape").decode("unicode-escape"))

Explanation for these two encodings: https://docs.python.org/3/library/codecs.html#text-encodings

Answered By: S.B
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.