Python-解压缩字符串格式运算符列表的简便方法吗? [英] Python - short way to unpack list for string formatting operator?
本文介绍了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, ..).
这篇关于Python-解压缩字符串格式运算符列表的简便方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文