Ex10 What was that

Escape Sequences

在 Ex9 里,介绍了两种显示多行字符串的方法。

  1. \n(backslash n)方法. Python读到\n,就会自动开启下一行。
  2. “”“(三引号),创建一个多行输入环境。 把方法一引申,可以得到更多类似的东西。比如,要在字符串中输入引号,但又不能让Python以为是字符串结束标记。可采用"或者'的方法。

示例代码 Ex10

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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 fat_cat

这里,\t是输出Tab。运行结果:

小结

这节重点介绍了Escape Sequences,常用的可以见下表:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
ESCAPE  | WHAT IT DOES(-)
\\          | Backslash(\)
\'          | Single-quote(')
\a          | ASCII bell(BEL)
\b          | 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  | Charater with 16-bit hex value xxxx (Unicode only)
\Uxxxxxxxx  | Character with 32-bit hex value xxxxxxxx (Unicode only)
\v          | ASCII vertical tab(VT)
\ooo        | Character with octal value ooo
\xhh        | Character with hex value hh
Licensed under CC BY-NC-SA 4.0
使用 Hugo 构建
主题 StackJimmy 设计