alo*_*lok 5 python unicode unicode-string python-2.7 python-unicode
我试图打印unicode字符给他们的名字如下:
# -*- coding: utf-8 -*-
print "\N{SOLIDUS}"
print "\N{BLACK SPADE SUIT}"
Run Code Online (Sandbox Code Playgroud)
然而,我得到的输出并不是很令人鼓舞.
转义序列按原样打印.
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}
>>>
Run Code Online (Sandbox Code Playgroud)
然而,我可以看到另一个提问者已经能够成功地做到这一点.
怎么了?
Thi*_*ter 16
这些序列只能在unicode字符串中工作,这是Python 3中唯一的字符串.所以在Python 2中你需要在字符串文字前加上一个u.
>>> print "\N{SOLIDUS} \N{BLACK SPADE SUIT}"
\N{SOLIDUS} \N{BLACK SPADE SUIT}
>>> print u"\N{SOLIDUS} \N{BLACK SPADE SUIT}"
/ ?
Run Code Online (Sandbox Code Playgroud)
来自文档的相关行:
\N{name}Unicode数据库中名为name的字符(仅限Unicode)
| 归档时间: |
|
| 查看次数: |
4955 次 |
| 最近记录: |