Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out number/name of unicode character in Python?

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"] 
like image 839
Piotr Lopusiewicz Avatar asked Oct 28 '12 03:10

Piotr Lopusiewicz


People also ask

How do you find the Unicode of a character in Python?

Use Unicode code points in strings: \x , \u , \U Each code is treated as one character. You can check it with the built-in function len() which returns the number of characters.

How do I identify Unicode characters?

Unicode is explicitly defined such as to overlap in that same range with ASCII. Thus, if you look at the character codes in your string, and it contains anything that is higher than 127, the string contains Unicode characters that are not ASCII characters. Note, that ASCII includes only the English alphabet.

How do I find Unicode values?

To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X.


1 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' 
like image 134
DSM Avatar answered Oct 14 '22 03:10

DSM