0

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 ?

Mayur Koshti
  • 1,794
  • 15
  • 20
  • Could you maybe further clarify your question. since running `print('\U0001d403')` gives the Capital D – Aryan Garg Jun 04 '21 at 13:32
  • The escape sequence is only useful in string *literals*. If you already have a `str` value, you probably just want a new string with the correct character already in it. – chepner Jun 04 '21 at 13:34
  • If you are looking for a way to convert this symbol into a normal capital D, you can do `import unicodedata` and then `unicodedata.normalize('NFKC', '\U0001d403')` results in `'D'`. – kaya3 Jun 04 '21 at 13:37

1 Answers1

2

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.

chepner
  • 497,756
  • 71
  • 530
  • 681