Python - 为字符串格式化运算符解包列表的简短方法? [英] Python - short way to unpack list for string formatting operator?

查看:27
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆