Skip to content
Advertisement

How to get Unicode input from user in Python?

this is the code: varUnicode = input('tEnter your Unicodent>') print('u{}'.format(varUnicode)) i want to get unicode input from user and print the character. in the above code python gives me an error.

Advertisement

Answer

u is an escape sequence recognized in string literals:

Escape sequences only recognized in string literals are:

Escape      Meaning                                  Notes
Sequence

N{name}    Character named name
            in the Unicode database                  (4)
uxxxx      Character with 16-bit hex value xxxx     (5)
Uxxxxxxxx  Character with 32-bit hex value xxxxxxxx (6)

Notes:

  1. Changed in version 3.3: Support for name aliases 1 has been added.
  2. Exactly four hex digits are required.
  3. Any Unicode character can be encoded this way. Exactly eight hex digits are required.

Use

varUnicode = input('tEnter your Unicodent>')
print('\u{}'.format(varUnicode.zfill(4)).encode('raw_unicode_escape').decode('unicode_escape'))

or (maybe better)

varUnicode = input('tEnter your Unicodent>')
print('\U{}'.format(varUnicode.zfill(8)).encode('raw_unicode_escape').decode('unicode_escape'))
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement