【发布时间】:2013-07-25 20:53:58
【问题描述】:
print '%d:%02d' % divmod(10,20)
得到我想要的结果:
0:10
然而
print '%s %d:%02d' % ('hi', divmod(10,20))
结果:
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print '%s %d:%02d' % ('hi', divmod(10,20))
TypeError: %d format: a number is required, not tuple
如何修复第二个打印语句以使其正常工作?
我认为有一个比
更简单的解决方案m = divmod(10,20)
print m[0], m[1]
或使用 python 3 或 format()。
我觉得我错过了一些明显的东西
【问题讨论】:
-
来自:stackoverflow.com/questions/1455602/… print "这是一个元组:%s" % (divmod(10,20),)
-
我认为这是一个好问题。没有什么明显的!在我看来,相当微妙。信息量很大。
标签: python tuples string-formatting