习题 10: 那是什么?
在习题 9 中我你接触了一些新东西。我让你看到两种让字符串扩展到多行的
方法。第一种方法是在月份之间用 \n
(back-slash n
)隔开。这两个字符的作用是
在该位置上放入一个“新行(new line)”字符。
使用反斜杠 \
(back-slash) 可以将难打印出来的字符放到字符串。针对不同的符号
有很多这样的所谓“转义序列(escape sequences)”,但有一个特殊的转义序列,就是 双反斜杠(double back-slash)
\\
。这两个字符组合会打印出一个反斜杠来。接下来我们做几个练习,然后你就知道这些转义序列的意义了。
另外一种重要的转义序列是用来将单引号 '
和双引号 "
转义。想象你有一个用
双引号引用起来的字符串,你想要在字符串的内容里再添加一组双引号进去,比如你想说
"I "understand" joe."
,Python 就会认为 "understand"
前后的两个引号是字符串
的边界,从而把字符串弄错。你需要一种方法告诉 python 字符串里边的双引号不是真正
的双引号。
要解决这个问题,你需要将双引号和单引号转义,让 Python 将引号也包含到字符串里边去。这里 有一个例子:
"I am 6'2\" tall." # 将字符串中的双引号转义
'I am 6\'2" tall.' # 将字符串种的单引号转义
第二种方法是使用“三引号(triple-quotes)”,也就是 """
,你可以在一组三引号之间放入
任意多行的文字。接下来你将看到用法。
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
What You Should See
注意你打印出来的制表符,这节练习中的文字间隔对于答案的正确性是很重要的。
$ python ex10.py
I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.
I'll do a list:
* Cat food
* Fishies
* Catnip
* Grass
Escape Sequences
这是所有Python支持的转义序列。您可能不会使用其中的许多,但无论如何都要记住它们的格式和他们做什么。试着输出来一些字符串,看看你是否可以让他们运行。
EscapeWhat it does. | |
---|---|
\\ | Backslash (\) |
\' | Single-quote (') |
\" | Double-quote (") |
\a | ASCII bell (BEL) |
\b | ASCII backspace (BS) |
\f | ASCII formfeed (FF) |
\n | ASCII linefeed (LF) |
\N{name} | Character named name in the Unicode database (Unicode only) |
\r | Carriage Return (CR) |
\t | Horizontal Tab (TAB) |
\uxxxx | Character with 16-bit hex value xxxx (u'' string only) |
\Uxxxxxxxx | Character with 32-bit hex value xxxxxxxx (u'' string only) |
\v | ASCII vertical tab (VT) |
\ooo | Character with octal value ooo |
\xhh | Character with hex value hh |
Note If you use \U or \u then you'll need to use a unicode string in u'\U0001F47E'. Put a u in front of the '' (single-quotes) or "" (double-quotes).
下面是一小块的有趣代码来尝试:
while True:
for i in ["/","-","|","\\","|"]:
print "%s\r" % i,
Study Drills
- 上网搜索一下还有哪些可用的转义字符。
- 使用
'''
(三个单引号)取代三个双引号,看看效果是不是一样的? - 将转义序列和格式化字符串放到一起,创建一种更复杂的格式。
- 记得
%r
格式化字符串吗?使用%r
搭配单引号和双引号转义字符打印一些字符串出来。 将 %r 和 %s 比较一下。 注意到了吗?%r 打印出来的是你写在脚本里的内容,而 %s 打印的是你应该看到的内容。
Common Student Questions
I still haven't completely figured out the last exercise. Should I continue? |
---|
Yes, keep going. Instead of stopping, take notes listing things you don't understand for each exercise. Periodically go through your notes and see if you can figure these things out after you've completed more exercises. Sometimes though you may need to go back a few exercises and do them again. |
What makes \ special compared to the other ones? |
It's simply the way you would write out one backslash () character. Think about why you would need this. |
When I write // or /n it doesn't work. |
That's because you are using a forward-slash / and not a backslash . They are different characters that do very different things. |
When I use a %r format none of the escape sequences work. |
That's because %r is printing out the raw representation of what you typed, which is going to include the original escape sequences. Use %s instead. Always remember this: %r is for debugging, %s is for displaying. |
I don't get Study Drill 3. What do you mean by "combine" escape sequences and formats? |
One concept I need you to understand is that each of these exercises can be combined to solve problems. Take what you know about format strings and write some new code that uses format strings and the escape sequences from this exercise. |
What's better, ''' or """? |
It's entirely based on style. Go with the ''' (triple-single-quote) style for now but be ready to use either depending on what feels best or what everyone else is doing. |
Copyright (C) 2010 by
Author: Zed Shaw
Translator:Zander Wong