I have a variable in which Unicode characters are typed with string
print(x) # output -> '\u062f\u0631 \u0627\u0628\u0644' print(type(x)) # output -> <class 'str'> how can i convert x in utf8 ?
Use .encode('raw_unicode_escape').decode('unicode_escape') for doubled Reverse Solidi, see Python Specific Encodings
x= '\\u062f\\u0631 \\u0627\\u0628\\u0644' print(x, '->', x.encode('raw_unicode_escape').decode('unicode_escape')) \u062f\u0631 \u0627\u0628\u0644 -> در ابل
x.encode('ascii').decode('unicode_escape') is sufficient. There's nothing to escape in the original string.x contains a non-ascii character, e.g. x= '"در ابل" is the same as "\\u062f\\u0631 \\u0627\\u0628\\u0644"'. So I'm on safer side…U+D800 to U+DFFF. I can't see any such codepoint(s) in your or my minimal reproducible example.