【问题标题】:Python str.format with string contatenation and continuationPython str.format 与字符串连接和延续
【发布时间】:2018-07-08 13:21:20
【问题描述】:

我想指定一个同时包含续行字符和连接字符的字符串。如果我要回显一堆相关值,这真的很有用。这是一个只有两个参数的简单示例:

temp = "here is\n"\
    +"\t{}\n"\
    +"\t{}".format("foo","bar")
print(temp)

这是我得到的:

here is
    {}
    foo

这是我所期望的:

here is
    foo
    bar

什么给了?

【问题讨论】:

    标签: python string format concatenation continuation


    【解决方案1】:

    你可以试试这样的:

    temp = ("here is\n"
            "\t{}\n"
            "\t{}".format("foo","bar"))
    print(temp)
    

    或者喜欢:

    # the \t have been replaced with
    # 4 spaces just as an example
    temp = '''here is
        {}
        {}'''.format
    
    print(temp('foo', 'bar'))
    

    对比你有什么:

    a = "here is\n"
    b = "\t{}\n"
    c = "\t{}".format("foo","bar")
    print( a + b + c)
    

    【讨论】:

    • 您的第二个解决方案将有 2 个额外的“\n”,您需要删除 2 个键入的“\n”。
    • 对于您的第二个代码块,您必须使用temp = '''\ 来使空格保持一致。
    【解决方案2】:

    str.format 在连接字符串之前被调用。可以把它想象成1 + 2 * 3,在加法之前先计算乘法。

    只需将整个字符串括在括号中,表示您希望在调用str.format之前将字符串连接起来:

    temp = ("here is\n"
          + "\t{}\n"
          + "\t{}").format("foo","bar")
    

    【讨论】:

    • 只需删除+ 连接,并留给编译器连接文字。
    • @MartijnPieters:如果这意味着更清晰的代码,我会考虑性能问题。隐式字符串连接给我带来了很多问题。
    【解决方案3】:

    Python 实际上看到了这一点:

    Concatenate the result of
        "here is\n"
    with the resuslt of
        "\t{}\n"
    with the result of
        "\t{}".format("foo","bar")
    

    您有 3 个单独的字符串文字,只有最后一个应用了 str.format() 方法。

    请注意,Python 解释器在运行时连接字符串。

    您应该改为使用隐式字符串文字连接。每当您将两个字符串文字并排放置在表达式中 之间没有其他运算符 时,您会得到一个字符串:

    "This is a single" " long string, even though there are separate literals"
    

    这与字节码一起存储为单个常量:

    >>> compile('"This is a single" " long string, even though there are separate literals"', '', 'single').co_consts
    ('This is a single long string, even though there are separate literals', None)
    >>> compile('"This is two separate" + " strings added together later"', '', 'single').co_consts
    ('This is two separate', ' strings added together later', None)
    

    来自String literal concatenation documentation

    允许多个相邻的字符串或字节文字(由空格分隔),可能使用不同的引用约定,并且它们的含义与它们的连接相同。因此,"hello" 'world' 等价于"helloworld"

    当您使用隐式字符串文字连接时,末尾的任何.format() 调用都会应用于该整个单个字符串

    接下来,您不想使用\ 反斜杠续行。改用括号,这样更简洁:

    temp = (
        "here is\n"
        "\t{}\n"
        "\t{}".format("foo","bar"))
    

    这叫implicit line joining

    您可能还想了解 多行 字符串文字,其中您在开头和结尾使用三个引号。此类字符串中允许换行符并保留为值的一部分:

    temp = """\
    here is
    \t{}
    \t{}""".format("foo","bar")
    

    我在 """ 开头后使用了一个 \ 反斜杠来转义第一个换行符。

    【讨论】:

      【解决方案4】:

      格式化函数只应用于最后一个字符串。

      temp = "here is\n"\
          +"\t{}\n"\
          +"\t{}".format("foo","bar")
      

      正在这样做:

      temp = "here is\n" + "\t{}\n"\ + "\t{}".format("foo","bar")
      

      关键是.format()函数只发生在最后一个字符串上:

      "\t{}".format("foo","bar")
      

      使用括号可以得到想要的结果:

      temp = ("here is\n"\
          +"\t{}\n"\
          +"\t{}").format("foo","bar")
      print(temp)
      
      #here is
      #   foo
      #   bar
      

      【讨论】:

        猜你喜欢
        • 2015-11-09
        • 2010-10-26
        • 2010-09-27
        • 2012-09-04
        • 1970-01-01
        • 2015-01-13
        • 2023-04-06
        • 2013-04-11
        • 1970-01-01
        相关资源
        最近更新 更多