【问题标题】:Can you have variables within triple quotes? If so, how?三引号内可以有变量吗?如果是这样,怎么做?
【发布时间】:2011-04-22 02:44:45
【问题描述】:

对于某些人来说,这可能是一个非常简单的问题,但它让我很难过。你可以在 python 的三引号中使用变量吗?

在下面的例子中,如何在文本中使用变量:

wash_clothes = 'tuesdays'
clean_dishes = 'never'

mystring =""" I like to wash clothes on %wash_clothes
I like to clean dishes %clean_dishes
"""

print(mystring)

我希望它产生:

 I like to wash clothes on tuesdays
     I like to clean dishes never

如果不是在需要几个变量并且有大量文本和特殊字符的情况下处理大块文本的最佳方法是什么?

【问题讨论】:

    标签: python string formatting


    【解决方案1】:

    Python 2 中的一种方式:

    >>> mystring =""" I like to wash clothes on %s
    ... I like to clean dishes %s
    ... """
    >>> wash_clothes = 'tuesdays'
    >>> clean_dishes = 'never'
    >>> 
    >>> print mystring % (wash_clothes, clean_dishes)
     I like to wash clothes on tuesdays
    I like to clean dishes never
    

    还要看看字符串格式

    【讨论】:

    • 那是完美的。非常感谢!这正是我一直在寻找的!不过,我不完全了解 python 如何知道哪个 %s 用哪个变量替换。它是否查看变量在以下出现的顺序: print mystring % (wash_clothes, clean_dishes)
    • @XL :您可以查看我提供链接但示例的其他格式设置技术。该示例由 NullUserException 提供。您可以同时使用这两种技术。
    【解决方案2】:

    是的。我相信这会奏效。

    do_stuff = "Tuesday"

    mystring = """I like to do stuff on %(tue)s""" % {'tue': do_stuff}

    编辑:在格式说明符中忘记了“s”。

    【讨论】:

    • 如果您要标记答案,请说明原因,以便我们都能从错误中吸取教训。
    • 这对于重复变量非常有用
    【解决方案3】:

    这样做的首选方法是使用str.format() 而不是使用% 的方法:

    这种字符串格式化方法是 Python 3.0 中的新标准,应该优先于新代码中的字符串格式化操作中描述的% 格式化。

    例子:

    wash_clothes = 'tuesdays'
    clean_dishes = 'never'
    
    mystring =""" I like to wash clothes on {0}
    I like to clean dishes {1}
    """
    
    print mystring.format(wash_clothes, clean_dishes)
    

    【讨论】:

    • 和 const string {, } 可以转义为 {{, }}
    【解决方案4】:

    我认为最简单的方法是 str.format() 就像其他人所说的那样。

    但是,我想我会提到 Python 有一个从 Python2.4 开始的 string.Template 类。

    这是文档中的一个示例。

    >>> from string import Template
    >>> s = Template('$who likes $what')
    >>> s.substitute(who='tim', what='kung pao')
    'tim likes kung pao'
    

    我喜欢这个的原因之一是使用映射而不是位置参数。

    【讨论】:

    • 这是一个有趣的方法。我真的不喜欢位置参数,因为如果您有大量变量,它可能会变得棘手。我唯一的问题是,如果模板字符串中有大量格式化文本和特殊字符,这种方法是否值得?
    • 您可以在format 中使用位置参数。 d = dict(foo=1, bar=2); "{foo} {bar}".format(**d)
    • string.Template 比其他方法慢几个数量级
    【解决方案5】:

    另请注意,您不需要中间变量:

    name = "Alain"
    print """
    Hello %s
    """ % (name)
    

    【讨论】:

      【解决方案6】:

      是的!从 Python 3.6 开始,您可以为此使用 f 字符串:它们被插入到位,因此 mystringmystring = ... 行之后将具有所需的值:

      wash_clothes = 'tuesdays'
      clean_dishes = 'never'
      
      mystring = f"""I like to wash clothes on {wash_clothes}
      I like to clean dishes {clean_dishes}
      """
      
      print(mystring)
      

      如果您需要在字符串中添加文字 {},您只需将其加倍:

      if use_squiggly:
          kind = 'squiggly'
      else:
          kind = 'curly'
      
      print(f"""The {kind} brackets are:
        - '{{', or the left {kind} bracket
        - '}}', or the right {kind} bracket
      """)
      

      会打印,取决于use_squiggly 的值,要么

      The squiggly brackets are:
        - '{', or the left squiggly bracket
        - '}', or the right squiggly bracket
      

      The curly brackets are:
        - '{', or the left curly bracket
        - '}', or the right curly bracket
      

      【讨论】:

      • 听起来不错 - 是否有可能将分隔符从大括号 {} 更改为其他内容?
      • 并且只需注意要在文本中添加单个 { 或 }(外部评估),您需要两次:{{ 或 }}
      • 在此之前,我一直认为 Python 字符串插值很笨拙。迟到总比没有好,嗯?
      【解决方案7】:

      以简单的方式传递多个参数

      wash_clothes = 'tuesdays'
      clean_dishes = 'never'
      a=""" I like to wash clothes on %s I like to clean dishes %s"""%(wash_clothes,clean_dishes)
      print(a)
      

      【讨论】:

        猜你喜欢
        • 2012-04-08
        • 2021-06-30
        • 2016-06-04
        • 2020-01-21
        • 2023-04-10
        • 2023-04-07
        • 2019-01-21
        • 2017-11-14
        • 2017-09-26
        相关资源
        最近更新 更多