【发布时间】:2014-02-18 20:06:15
【问题描述】:
我有一个多行字符串,我想用我自己的变量更改它的某些部分。我不太喜欢使用+ 运算符拼凑相同的文本。有没有更好的选择?
例如(需要内部引号):
line = """Hi my name is "{0}".
I am from "{1}".
You must be "{2}"."""
我希望能够多次使用它来形成一个更大的字符串,看起来像这样:
Hi my name is "Joan".
I am from "USA".
You must be "Victor".
Hi my name is "Victor".
I am from "Russia".
You must be "Joan".
有没有办法做类似的事情:
txt == ""
for ...:
txt += line.format(name, country, otherName)
【问题讨论】:
标签: python string coding-style formatting