0

I have recently had to do something with characters that were written with \xAB escape syntax, and noticed that there is a different behavior for different characters:

>>>'\x61'
'a'
>>>'\x10'
'\x10'
>>>print('\x61')
a
>>>print('\x10')


My question is: why >>> '\x10' doesn't yield ''?

1
  • 1
    pressing enter is like calling print(repr(x)) Commented Jan 15, 2020 at 14:30

1 Answer 1

4

They are interpreted the same. The difference is in the representation defined by str.__repr__, which prefers to use printable ASCII characters where possible.

print(x) uses x.__str__, not x.__repr__, which simply returns the string itself, and then writes that string to the terminal, at which point it is up to the terminal, not Python, to decide how to display the result.

\x10, in particular, indicates the control character DLE (Data Link Escape), which doesn't have any particular meaning to the terminal, so it is typically left undisplayed or, in some cases, shown with some generic placeholder like ?.

Sign up to request clarification or add additional context in comments.

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.