Search code examples
pythonunicodeunicode-escapes

How can I get python ''.encode('unicode_escape') to return escape codes for ascii?


I am trying to use the encode method of python strings to return the unicode escape codes for characters, like this:

>>> print( 'ф'.encode('unicode_escape').decode('utf8') )
\u0444

This works fine with non-ascii characters, but for ascii characters, it just returns the ascii characters themselves:

>>> print( 'f'.encode('unicode_escape').decode('utf8') )
f

The desired output would be \u0066. This script is for pedagogical purposes.

How can I get the unicode hex codes for ALL characters?


Solution

  • ord can be used for this, there is no need for encoding/decoding at all:

    >>> '"\\U{:08x}"'.format(ord('f'))  # ...or \u{:04x} if you prefer
    '"\\U00000066"'
    >>> eval(_)
    'f'