How to find out number/name of unicode character in Python?
Question:
In Python:
>>> "N{BLACK SPADE SUIT}"
'♠'
>>> "u2660"
'♠'
Now, let’s say I have a character which I don’t know the name or number for. Is there a Python function which gives this information like this?
>>> wanted_function('♠')
["BLACK SPADE SUIT", "u2660"]
Answers:
You may find the unicodedata module handy:
>>> s = "N{BLACK SPADE SUIT}"
>>> s
'♠'
>>> import unicodedata
>>> unicodedata.name(s)
'BLACK SPADE SUIT'
>>> ord(s)
9824
>>> hex(ord(s))
'0x2660'
In Python:
>>> "N{BLACK SPADE SUIT}"
'♠'
>>> "u2660"
'♠'
Now, let’s say I have a character which I don’t know the name or number for. Is there a Python function which gives this information like this?
>>> wanted_function('♠')
["BLACK SPADE SUIT", "u2660"]
You may find the unicodedata module handy:
>>> s = "N{BLACK SPADE SUIT}"
>>> s
'♠'
>>> import unicodedata
>>> unicodedata.name(s)
'BLACK SPADE SUIT'
>>> ord(s)
9824
>>> hex(ord(s))
'0x2660'