我已经测试了一些转义序列。他们中的大多数工作,但他们中的一些人没有。为什么是这样?
这是我为测试转义序列而编写的代码:
# -*- coding: UTF-8 -*-
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split \non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = '''
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
'''
print tabby_cat
print persian_cat
print backslash_cat
print fat_cat
#while True:
# for i in ["/", "-", "|", "\\", "|"]:
# print "%s\r" % i,
print "This should ring a bell \a"
print "Does this have a backspace\b"
print "Printing \"double quotes\" and some \'single quotes\'"
print "Formfeed.\fWill this work?"
print "Linefeed.\nWill this work?"
print "Print the character named name: \N{name}"
print "Print a carriage return.\rWill this work?"
print "A 16 bit unicode character: \u6C34"
print "Let's check this vertical tab: \v\vWhat is this?"
print "This should output an octal value: \111"
print "This should output a hex value: \xAA"
终端中的输出:
I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.
I'll do a list:
* Cat food
* Fishies
* Catnip
* Grass
This should ring a bell
Does this have a backspace
Printing "double quotes" and some 'single quotes'
Formfeed.
Will this work?
Linefeed.
Will this work?
Print the character named name: \N{name}
Will this work?e return.
A 16 bit unicode character: \u6C34
Let's check this vertical tab:
What is this?
This should output an octal value: I
This should output a hex value: �
那些不起作用的是:
- 钟:
\a
- 退格:
\b
- 人物名称:(
\N{name}
已解决) - 16 位和 32 位 unicode 字符:(
\u6C34
已解决)
我应该怎么做才能使这些转义序列起作用?
================编辑=====================
print u"Print the character named name: \N{BLACK SPADE SUIT}"
print u"A 16 bit unicode character: \u6C34"
Print the character named name: ♠
A 16 bit unicode character: 水
- 16 位和 32 位 Unicode 字符:
\u6C34
- 字符名称:
\N{name}
在字符串前面加上u