如何在包含'{}'
的字符串中给出参数?示例:string = "my age is '{}' years old'
。我们可以通过->print(string.format(23))
,则输出为->my age is 23 years old
。
我的问题是,当我们在一个字符串中有多个'{}'
时,我们必须向'{}'
发送多个参数,这些参数在列表或像(1, 2,3)
这样的元组中。我们如何将.format()
应用于此?
我在解决这个问题时有这个疑问->问题:I am 12 years 3 months 8 days old
到所需输出:I am 3 years 8 months 12 days old
。(数字应按排序形式排列(。
string = "I am {} years {} months {} days old".format((12),(3),(8))
或
string = "I am {} years {} months {} days old".format(*(12,3,8))