Search code examples
pythonpython-3.xunicode

Is it possible to convert various Unicode escape sequences?


From the Python Unicode HOWTO:

>>> s = "a\xac\u1234\u20ac\U00008000"
... #     ^^^^ two-digit hex escape
... #         ^^^^^^ four-digit Unicode escape
... #                     ^^^^^^^^^^ eight-digit Unicode escape

>>> print(s)
a¬ሴ€耀

Is it possible to express the Unicode 4 escape sequence "\u20ac" with only hex escapes? Why, for example wouldn't "\x20\xac" work?

And the same for the escape sequence \U00008000". Would it be possible to write that with only a hex- or Unicode 4 escape?


Solution

  • >>> "\u20ac"
    '€'
    >>> "\x20\xac"
    ' ¬'
    >>> "\U00008000"
    '耀'
    >>> "\u8000"
    '耀'
    >>> "\U00008000" == "\u8000"
    True
    

    let us know if you have more questions ;)