Python 中的三引号
pythonserver side programmingprogramming更新于 2025/3/31 17:16:17
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 ( ) 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. """ 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
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

