在 Python 中,您可以在三引号内添加变量吗?如果是这样,如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3877623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Python, can you have variables within triple quotes? If so, how?
提问by XL.
This is probably a very simple question for some, but it has me stumped. Can you use variables within python's triple-quotes?
对于某些人来说,这可能是一个非常简单的问题,但它让我难住了。你可以在python的三重引号中使用变量吗?
In the following example, how do use variables in the text:
在下面的例子中,如何在文本中使用变量:
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 would like it to result in:
我希望它导致:
I like to wash clothes on tuesdays
I like to clean dishes never
If not what is the best way to handle large chunks of text where you need a couple variables, and there is a ton of text and special characters?
如果不是,在需要几个变量并且有大量文本和特殊字符的情况下,处理大块文本的最佳方法是什么?
采纳答案by pyfunc
One of the ways in Python 2 :
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
Also look at string formatting
还要看字符串格式
回答by jonesy
Yes. I believe this will work.
是的。我相信这会奏效。
do_stuff = "Tuesday"
do_stuff = "Tuesday"
mystring = """I like to do stuff on %(tue)s""" % {'tue': do_stuff}
mystring = """I like to do stuff on %(tue)s""" % {'tue': do_stuff}
EDIT: forgot an 's' in the format specifier.
编辑:忘记了格式说明符中的“s”。
回答by NullUserException
The preferred way of doing this is using str.format()rather than the method using %:
这样做的首选方法是使用str.format()而不是使用的方法%:
This method of string formatting is the new standard in Python 3.0, and should be preferred to the
%formatting described in String Formatting Operations in new code.
这种格式化字符串的方法是 Python 3.0 中的新标准,应该优先
%于新代码中字符串格式化操作中描述的格式化。
Example:
例子:
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)
回答by chauncey
I think the simplest way is str.format() as others have said.
我认为最简单的方法是 str.format() 正如其他人所说。
However, I thought I'd mention that Python has a string.Templateclass starting in Python2.4.
但是,我想我会提到 Python 有一个从 Python2.4 开始的string.Template类。
Here's an example from the docs.
这是文档中的一个示例。
>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
One of the reasons I like this is the use of a mapping instead of positional arguments.
我喜欢这个的原因之一是使用映射而不是位置参数。
回答by Alain Collins
Also note that you don't need the intermediate variable:
另请注意,您不需要中间变量:
name = "Alain"
print """
Hello %s
""" % (name)
回答by Antti Haapala
Yes! Starting from Python 3.6 you can use the fstrings for this: They're interpolated in place, so the mystringis already the required output.
是的!从 Python 3.6 开始,您可以f为此使用字符串:它们被插入到位,因此mystring已经是所需的输出。
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)
回答by Alkesh Mahajan
Pass multiple args in simple way
以简单的方式传递多个参数
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)

