I'm trying to print the literal unicode escape character of "Pedro Le\u00F3n". When I do the following:
test = "Pedro Le\u00F3n"
print(test)
>>> Pedro León
How can I get it to output "Pedro Le\u00F3n"?
Encode to bytes with the unicode_escape encoding, and decode right back:
>>> out = test.encode('unicode_escape').decode()
>>> out
'Pedro Le\\xf3n'
>>> print(out)
Pedro Le\xf3n
Note that it's a \xXX escape instead of a \uXXXX escape, since it's less than U+FF. For comparison:
>>> '\u0080'
'\x80'
You need to use raw strings. Simply use r before your string:
test = r"Pedro Le\\u00F3n"
print(test)
Output: Pedro Le\\u00F3n