转义字符
| <td style="border-color:#bfbfbf;vertical-align:top;width:207.4pt;">
<p style="margin-left:0cm;"><strong>打印</strong></p>
</td>
</tr><tr><td style="border-color:#bfbfbf;width:207.4pt;">
<p style="margin-left:0cm;"><strong>\’</strong></p>
</td>
<td style="width:207.4pt;">
<p style="margin-left:0cm;">单引号</p>
</td>
</tr><tr><td style="border-color:#bfbfbf;vertical-align:top;width:207.4pt;">
<p style="margin-left:0cm;"><strong>\”</strong></p>
</td>
<td style="vertical-align:top;width:207.4pt;">
<p style="margin-left:0cm;">双引号</p>
</td>
</tr><tr><td style="border-color:#bfbfbf;width:207.4pt;">
<p style="margin-left:0cm;"><strong>\t</strong></p>
</td>
<td style="width:207.4pt;">
<p style="margin-left:0cm;">制表符</p>
</td>
</tr><tr><td style="border-color:#bfbfbf;vertical-align:top;width:207.4pt;">
<p style="margin-left:0cm;"><strong>\n</strong></p>
</td>
<td style="vertical-align:top;width:207.4pt;">
<p style="margin-left:0cm;">换行符</p>
</td>
</tr><tr><td style="border-color:#bfbfbf;width:207.4pt;">
<p style="margin-left:0cm;"><strong>\\</strong></p>
</td>
<td style="width:207.4pt;">
<p style="margin-left:0cm;">倒斜杠</p>
</td>
</tr></tbody></table>
表6-1转义字符
在交互式环境中输入以下代码:
>>> print("Hello there!\nHow are you?\nI'm doing fine.")
Hello there!
How are you?
I'm doing fine.
6.1.3. 原始字符串
可以在字符串开始的引号之前加上n使它成为原始字符串。
“原始字符串”完全忽略所有的转义字符, 打印出字符串中所有的倒斜杠。例如,
在交互式环境中输入以下代码:
>>> print(r'That is Carol\'s cat.')
That is Carol's cat.
因为这是原始字符串,Python认为倒斜杠是字符串的一部分,
而不是转义字符的开始。如果输入的字符串包含许多倒斜杠,
比如下一章中要介绍的正则表达式字符串,那么原始字符串就很有用。
6.1.4. 用三重引号的多行字符串
虽然可以用 \n
转义字符将换行放入一个字符串,
但使用多行字符串通常更容易。在Python中,
多行字符串的起止是3个单引号或3个双引号。
“三重引号”之间的所有引号、制表符或换行,
都被认为是字符串的一部分。Python的代码块缩进规则不适用于多行字符串。
打开文件编辑器,输入以下代码:
>>> print('''Dear Alice,
>>>
>>> Eve's cat has been arrested for catnapping, cat burglary, and extortion.
>>>
>>> Sincerely,
>>> Bob''')
Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob
将该程序保存为 catnapping.py
并运行。
请注意, Eve's
中的单引号字符不需要转义。
在原始字符串中,转义单引号和双引号是可选的。 下面的 print()
调用将打印出同样的文本, 但没有使用多行字符串:
>>> print('Dear Alice,\n\nEve\'s cat has been arrested for catnapping, cat burglary, and extortion.\n\nSincerely,\nBob')
Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob
6.1.5. 多行注释
虽然井号字符(#
)表示这一行是注释,但多行字符串常常用作多行注释。
下面是完全有效的 Python 代码:
>>> """This is a test Python program.
>>> Written by Al Sweigart al@inventwithpython.com
>>> This program was designed for Python 3, not Python 2.
>>> """
>>> def spam():
>>> """This is a multiline comment to help
>>> explain what the spam() function does."""
>>> print('Hello!')
6.1.6. 字符串下标和切片
字符串像列表一样,使用下标和切片。 可以将字符串 'Hello world!'
看成是一个列表, 字符串中的每个字符都是一个表项,有对应的下标。
' H e l l o w o r l d ! '
0 1 2 3 4 5 6 7 8 9 10 11
字符计数包含了空格和感叹号, 所以 'Hello world!'
有12个字符, H
的下标是0, !的下标是11。 在交互式环境中输入以下代码:
>>> spam = 'Hello world!'
>>> spam[0]
如果指定一个下标,你将得到字符串在该处的字符。
如果用一个下标和另一个下标指定一个范围,
开始下标将被包含,结束下标则不包含。因此, 如果 spam
是
'Hello world!'
, spam[0:5]
就是'Hello'
。 通过
spam[0:5]
得到的子字符串, 将包含 spam[0]
到 spam[4]
的全部内容, 而不包括下标5处的空格。
请注意,字符串切片并没有修改原来的字符串。
可以从一个变量中获取切片,记录在另一个变量中。
在交互式环境中输入以下代码:
>>> spam = 'Hello world!'
>>> fizz = spam[0:5]
>>> fizz
通过切片并将结果子字符串保存在另一个变量中,
就可以同时拥有完整的字符串和子字符串, 便于快速简单的访问。
6.1.7. 字符串的 in
和 not in
操作符
像列表一样, in
和 not in
操作符也可以用于字符串。 用 in
或
not in
连接两个字符串得到的表达式, 将求值为布尔值 True
或
False
。 在交互式环境中输入以下代码:
>>> 'Hello' in 'Hello World'
>>> 'HELLO' in 'Hello World'
>>> 'cats' not in 'cats and dogs'
这些表达式测试第一个字符串(精确匹配,区分大小写)是否在第二个字符串中。