如何使用具有任意数量元素的占位符创建python字符串

bio*_*med 4 python placeholder

我可以

string="%s"*3
print string %(var1,var2,var3)
Run Code Online (Sandbox Code Playgroud)

但我无法将变量转换为另一个变量,以便我可以使用app逻辑动态创建变量列表.例如

if condition:
  add a new %s to string variable
  vars.append(newvar)

else:
  remove one %s from string
  vars.pop()
print string with placeholders
Run Code Online (Sandbox Code Playgroud)

有关如何使用python 2.6执行此操作的任何想法?

Omn*_*ous 6

这个怎么样?

print ("%s" * len(vars)) % tuple(vars)
Run Code Online (Sandbox Code Playgroud)

实际上,这是一种相当愚蠢的做事方式.如果您只想将所有变量压缩在一个大字符串中,这可能是一个更好的主意:

print ''.join(str(x) for x in vars)
Run Code Online (Sandbox Code Playgroud)

这确实需要至少Python 2.4才能工作.