这可能是最简单的问题。但我尝试以下列方式打印元组的单个值。
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:21:18
与其将printf-style格式化和str.format混合,选择一个:
>>> mytuple = ('new','lets python','python 2.7')
>>> print "%s experience, %s with %s" % mytuple
new experience, lets python with python 2.7>> print "{0} experience, {1} with {2}".format(*mytuple) new experience, lets python with python 2.7)">str.format
>>> print "{0} experience, {1} with {2}".format(*mytuple)
new experience, lets python with python 2.7发布于 2016-05-16 02:27:57
您可以使用格式方法和星号来解决这个问题。
详情请参阅此链接
>>mytuple=('new','lets python','python 2.7')
>>print "{0} experience, {1} with {2} ".format(*mytuple)
new experience, lets python with python 2.7 发布于 2016-05-16 02:29:39
你可以用任何一种方法。然而,格式的事情更简单,您可以更容易地管理它。
>>> a = '{0} HI {1}, Wassup {2}'
>>> a.format('a', 'b', 'c')
'a HI b, Wassup c'
>>> b = ('a' , 'f', 'g')
>>> a.format(*b)
'a HI f, Wassup g'https://stackoverflow.com/questions/37245791
复制相似问题