[英]Python - short way to unpack list for string formatting operator?
不幸的是,*或**运算符的变化似乎不起作用:
lstData = [1,2,3,4]
str = 'The %s are %d, %d, %d, and %d' % ('numbers', *lstData)
有一个简单的方法吗?
使用格式 :
str = 'The {} are {}, {}, {}, and {}'.format('numbers', *lstData)
有关可能的格式(浮点数,小数点,转换,...)的更多详细信息,请参阅文档。
s = 'The %s are %d, %d, %d, and %d' % tuple(['numbers'] + lstData)
>>> data = range(5)
>>> 'The {0} are {1}, {2}, {3}, {4} and {5}'.format('numbers', *data)
'The numbers are 0, 1, 2, 3 and 4'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.