【问题标题】:Is there a clean way to write multi-lines strings in Python? [duplicate]有没有一种干净的方法可以在 Python 中编写多行字符串? [复制]
【发布时间】:2014-02-28 12:50:07
【问题描述】:

这听起来像是一个初学者的问题,但我从来没有成功地在 Python 中以干净的方式编写长字符串。

以下是我列出的 4 种方法。在我看来,它们都没有问题。

def useless_func():
    # WRONG WAY A : string_A displays well but breaks the 80 char max PEP 8 recommandation
    string_A = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

    # WRONG WAY B : string_B will create unwanted spaces between word 'sed' and 'do' when printed
    string_B = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed\
        do eiusmod tempor incididunt ut labore et dolore magna aliqua."

    # WRONG WAY C : string_C displays well  but makes my code ugly because it breaks indentation
    string_C = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed\
do eiusmod tempor incididunt ut labore et dolore magna aliqua."

    # WRONG WAY D : string_D (triples quotes) has the same problem than string_B (unwanted spaces)
    string_D = '''Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
        do eiusmod tempor incididunt ut labore et dolore magna aliqua.'''

我错过了什么吗?

【问题讨论】:

    标签: python python-2.7


    【解决方案1】:

    我会选择:

    def pr():
        # parentheses are for grouping and (as a bonus) for a pretty indentation
        s = ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
             "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
        print s
    

    引用informal introduction to Python

    彼此相邻的两个字符串文字会自动连接; 上面的第一行也可以写成 word = 'Help' 'A'; 这仅适用于两个文字,不适用于任意字符串 表达式。

    >>> s = 'a' 'b'
    >>> s
    'ab'
    >>> s = 'a''b' # space is not necessary
    >>> s
    'ab'
    

    附注:在编译为字节码期间执行连接:

    >>> import dis
    >>> dis.dis(pr)
    
     0 LOAD_CONST               1 ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')
    

    可能这种串联传统来自C:

    // prints "hello world"
    #include <stdio.h>
    
    int main(int argc, char **argv) {
      printf("hello" " world");
      return 0;
    }
    

    【讨论】:

    • 这绝对是 5 种方法中最干净的方法 :)
    【解决方案2】:

    你可以试试:

    string = "sdfsdfsdfsdfsdf" \
             "sdfsdfsdfsdfs"
    

    结果:

    >>> string
    'sdfsdfsdfsdfsdfsdfsdfsdfsdfs'
    

    正如@Nigel Tufnel 在他的回答中提到的那样,使用括号代替\ 可以达到相同的效果。

    【讨论】:

      【解决方案3】:

      如何使用双引号或单三引号:

      >>> string_A = """Lorem ipsum dolor sit amet,
      ... this is also my content
      ... this is also my content
      ... this is also my content"""
      >>>
      >>> print string_A
      Lorem ipsum dolor sit amet,
      this is also my content
      this is also my content
      this is also my content
      >>>
      

      【讨论】:

        【解决方案4】:

        我认为这归结为您获得了多少屏幕空间。

        你可以使用连接..

        string_a = "this is my content"
        string_a += "this is also my content"
        

        【讨论】:

        • 但这会进行 2 次操作而不是 1 次
        猜你喜欢
        • 2015-03-08
        • 1970-01-01
        • 2020-05-18
        • 2010-10-03
        • 2021-10-14
        • 2019-07-02
        • 1970-01-01
        • 1970-01-01
        • 2011-06-12
        相关资源
        最近更新 更多