【问题标题】:How to make paragraphs readable within python code [duplicate]如何在python代码中使段落可读[重复]
【发布时间】:2014-10-14 08:09:05
【问题描述】:

我经常需要向用户展示文本段落。我对向用户呈现文本的知识感到满意(通常使用\n 或文本换行以使其在用户端令人愉悦)但是有没有办法在我的实际代码中使用回车而不需要\n

a = "this is a really long line of text that i need to show to the user. this is a really long line of text that i need to show to the user. this is a really long line of text that i need to show to the user. this is a really long line of text that i need to show to the user."

b = "this is a really long line of text that i need to show to the user.\n
this is a really long line of text that i need to show to the user.\n
this is a really long line of text that i need to show to the user.\n"

所以我的目标是在我的代码中将“a”分解成美观的块,但仍然使用自动换行向用户显示文本。我知道这样做的唯一方法显示在“b”中,但这不适用于自动换行

【问题讨论】:

    标签: python code-formatting


    【解决方案1】:

    所以您希望它在您的实际代码中可读吗? Python 将隐式连接代码中的相邻字符串。要让它跨行工作,您可以使用行继续符 \,或者最好将其括在括号中。

    a = ("This is a really long\n"
         "line of text that I\n"
         "need to show to the user.")
    
    print(a)
    

    【讨论】:

    • iirc 您不需要将其包裹在括号中或使用\,相邻的字符串跨空格连接。虽然它已经永远......编辑:不,我错了。括号赢了!
    • @AdamSmith 目前对我不起作用,但这可能是 IPython 的事情。不过,作为样式点,您可能应该无论如何都包含括号。
    • @AdamSmith 你可以通过在eval 中嵌入sn-p 来证明任何repl 中需要parens。没有括号,这是一个语法错误。
    【解决方案2】:

    将字符串放在括号中,然后利用 Python 自动连接相邻字符串文字。然后,您可以在代码中的文本周围放置任何空格格式。

    b = ("this is a really long line of text that i need to show to the user."
         "this is a really long line of text that i need to show to the user."
         "this is a really long line of text that i need to show to the user.")
    

    【讨论】:

      【解决方案3】:

      您可以使用三引号,例如

      b = """this is a really long line of text that i need to show to the user.
      this is a really long line of text that i need to show to the user.
      this is a really long line of text that i need to show to the user."""
      # no \n needed, but carriage returns happen on every newline
      

      或者你可以使用自动字符串连接

      # THIS DOESN'T WORK
      b = "this is a really long line of text that i need to show to the user."
          "this is a really long line of text that i need to show to the user."
          "this is a really long line of text that i need to show to the user."
      # also no \n needed, but this is one long line of text.
      

      哎呀,Roger Fan 的出色回答中的 cmets 提醒我,如果不将其括在括号中或使用续行符 \,则无法做到这一点

      b = ("this is a really long line of text that i need to show to the user."
           "this is a really long line of text that i need to show to the user."
           "this is a really long line of text that i need to show to the user.")
      # OR
      b = "this is a really long line of text that i need to show to the user."\
          "this is a really long line of text that i need to show to the user."\
          "this is a really long line of text that i need to show to the user."
      

      【讨论】:

        【解决方案4】:

        您可以使用带三引号的“heredoc”:

        b = 
        '''
        this is a really long line of text that i need to show to the user.
        this is a really long line of text that i need to show to the user.
        this is a really long line of text that i need to show to the user.
        '''
        

        【讨论】:

        • 我想你会发现这是一个语法错误...
        • @NedBatchelder 请通过提供正确的语法帮助改进我的答案。
        猜你喜欢
        • 1970-01-01
        • 2016-08-09
        • 1970-01-01
        • 2010-10-15
        • 2014-09-30
        • 1970-01-01
        • 1970-01-01
        • 2012-10-07
        • 2021-06-14
        相关资源
        最近更新 更多