Python - short way to unpack list for string formatting operator?

巧了我就是萌 提交于 2019-12-01 04:44:18

Use format:

str = 'The {} are {}, {}, {}, and {}'.format('numbers', *lstData)

see the docs for more details about possible formatting (floats, decimal points, conversion, ..).

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'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!