多行字符串

时间:2015-07-11 13:40:32

标签: python string performance

假设,我想定义一个两行(或多行)字符串。

我可以通过两种方式做到这一点:

  • 对换行符使用转义序列。\n
    例如:"This is the first sentence. \n This is the second sentence."
  • 使用三引号字符串。
    例如:""" This is the first sentence. This is the second sentence."""

哪种效率更高或更传统?为什么?

1 个答案:

答案 0 :(得分:0)

我很想说它并不重要,因为每个人在解析文本时仍会扫描内部的转义字符。

>>> print "a\n\tb"
a
        b
>>> print """a\n\tb"""
a
        b