1

我有一个列表/数组,我想对其进行字符串格式化。例如,

 a = [1, 2, 3] and output that I want should be 010203

我试过了b = "%.2d %(a)",但是,它给了我一个错误TypeError: %d format: a number is required, not list。当然,我可以理解这个错误,但是,我不知道其他方式。

代码应该是通用的,我的意思是,当我的列表像a = [10, 11, 12, 13]时,它应该打印输出像10111213.

4

1 回答 1

2
>>> a = [1,2,3]
>>> "".join("%.2d" % i for i in a)
'010203'
>>> a = [10, 11, 12, 13]
>>> "".join("%.2d" % i for i in a)
'10111213'
于 2013-10-01T10:39:21.750 回答