Python 三引号
Python 的三引号允许字符串跨越多行,包括逐字 NEWLINE、TAB 和任何其他特殊字符,因而可以派上用场。
三引号的语法包括三个连续的单引号或双引号。
示例
#!/usr/bin/python para_str = """this is a long string that is made up of several lines and non-printable characters such as TAB ( \t ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ \n ], or just a NEWLINE within the variable assignment will also show up. """ print para_str
输出
执行上述代码时,将产生以下结果。
请注意,每个特殊字符如何转换为其打印形式,一直到 "up。"结尾和关闭的三引号之间的字符串末尾的最后一个 NEWLINE。另外请注意,NEWLINE 可能会出现在行尾的显式回车或其转义码 (\n) 中 −
this is a long string that is made up of several lines and non-printable characters such as TAB ( ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ ], or just a NEWLINE within the variable assignment will also show up.
原始字符串根本不将反斜杠视为特殊字符。输入原始字符串的每个字符都将保持原样 −
示例
#!/usr/bin/python print 'C:\nowhere'
输出
执行上述代码时,将产生以下结果 −
C:\nowhere
现在,让我们使用原始字符串。我们将表达式置于 r'expression' 中,如下所示 −
示例
#!/usr/bin/python print r'C:\nowhere'
输出
执行上述代码时,将产生以下结果 −
C:\nowhere
广告