Python - 为字符串格式化运算符解包列表的简短方法?
问题描述
不幸的是,* 或 ** 运算符的变体似乎不起作用:
Variations of the * or ** operators don't seem to work, unfortunately:
lstData = [1,2,3,4]
str = 'The %s are %d, %d, %d, and %d' % ('numbers', *lstData)
有什么简单的方法吗?
解决方案
使用格式一个>:
str = 'The {} are {}, {}, {}, and {}'.format('numbers', *lstData)
有关可能的格式(浮点数、小数点、转换......)的更多详细信息,请参阅文档.
see the docs for more details about possible formatting (floats, decimal points, conversion, ..).
相关文章