1

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"?

3 Answers 3

2

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'
Sign up to request clarification or add additional context in comments.

Comments

1

You need to use raw strings. Simply use r before your string:

test = r"Pedro Le\\u00F3n"
print(test)

Output: Pedro Le\\u00F3n

1 Comment

Why did you put a double-backslash?
0

try this

test = "Pedro Le\\u00F3n"
print(test)

it causes because in python there are many "special characters like" '\n' and more and if you want to ignore it you need to write \ \ instead of \

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.