这可能是最简单的问题。但我尝试以下列方式打印元组的单个值。
mytuple=('new','lets python','python 2.7')
>>> print "%{0} experience, %{1} with %{2} " %mytuple
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
print "%{0} experience, %{1} with %{2} " %mytuple
ValueError: unsupported format character '{' (0x7b) at index 1我想打印输出如下所示。
"new experience, lets python with python 2.7"我不记得它是在哪里做的。它叫解包元组值,打印格式的元组。
发布于 2016-05-16 02:51:10
你只是把printf和str.format混为一谈,你需要选择其中之一:
>>> tuple1 = ("hello", "world", "helloworld")
>>> print("%s, %s, %s" % tuple1)或者:
>>> tuple1 = ("hello", "world", "helloworld")
>>> print("{}, {}, {}".format(*tuple1))https://stackoverflow.com/questions/37245791
复制相似问题