Search code examples
pythondecodeencodepython-unicodeunicode-escapes

How to convert a unicode character "\U0001d403" to Escape sequence in python?


It is a Mathematical Alphanumeric Symbols ( capital D ) and I just want to convert into simple English letter ( Capital D ).

Like, \U0001d403 => \uxxxx.

I am not familiar with decode-encoding mechanism.

Is there any conversion way ?


Solution

  • If nothing else, you can build your own map for the string translation: for example:

    >>> x = '\U0001d403'
    >>> x
    '𝐃'
    >>> x.translate(str.maketrans({'\U0001d403': 'D'}))
    'D'
    

    maketrans can create a mapping of multiple characters, which can be saved to be reused as an argument for many calls to str.translate. Note also that str.translate works for arbitrary strings; the given map will be applied to each character separately.