pythonstringunicodeescaping

Convert a string into unicode escape sequences


How can I convert a string so that each character is replaced with the corresponding Unicode escape sequence?

Something like:

def unicode_escape(s):
    """How do I write this?"""

print(unicode_escape("Hello, World!\n"))
# should print \u0048\u0065\u006C\u006C\u006F\u002C\u0020...

Solution

  • The ord() function returns the Unicode code point of a character. Just format this as \u followed by a 4-digit hex representation of that.

    def unicode_escape(s):
        return "".join(map(lambda c: rf"\u{ord(c):04x}", s))
    print(unicode_escape("Hello, World!\n"))
    # prints \u0048\u0065\u006c\u006c\u006f\u002c\u0020\u0057\u006f\u0072\u006c\u0064\u0021\u000a