Python escape sequence \ N {name} not working as defined

I am trying to print Unicode characters by specifying their name as follows:

# -*- coding: utf-8 -*-
print "\N{SOLIDUS}"
print "\N{BLACK SPADE SUIT}"

However, the conclusion I am getting is not very encouraging.

The control sequence is printed as is.

ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on
Type "help", "copyright", "credits" or "license" for more information.
>>> # -*- coding: utf-8 -*-
... print "\N{SOLIDUS}"
\N{SOLIDUS}
>>> print "\N{BLACK SPADE SUIT}"
\N{BLACK SPADE SUIT}
>>>

However, I can see that another questionnaire was able to do this successfully.

What's wrong?

+5
source share
1 answer

These sequences only work on unicode strings , which is the only type of Python 3 string. So in Python 2 you need a string literal prefix with u.

>>> print "\N{SOLIDUS} \N{BLACK SPADE SUIT}"
\N{SOLIDUS} \N{BLACK SPADE SUIT}
>>> print u"\N{SOLIDUS} \N{BLACK SPADE SUIT}"
/ ♠

Corresponding line from the documents:

\N{name} Named symbol name in Unicode database (Unicode only)

+14

All Articles