我有一個多行字符串,我想用自己的變量改變它的某些部分。我真的不喜歡用+運算符拼湊相同的文本。有更好的選擇嗎?在Python中格式化多行字符串的最優雅的方式
例如(內部引號是必需的):
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)