python - 在python中使用字符串格式,生成重复的短信
问题描述
我有:
inventory = ["shoes, 12, 29.99", "shirts, 20, 9.99", "sweatpants, 25, 15.00", "scarves, 13, 7.75"]
for item in inventory:
print('The store has {1} {0}, each for {2} USD'.format(item))
我想得到:商店有 12 只鞋子,每只 29.99 美元。. .
解决方案
您需要拆分item
in for 循环,以便可以在.format
方法中使用这些值。
inventory = ["shoes, 12, 29.99", "shirts, 20, 9.99", "sweatpants, 25, 15.00", "scarves, 13, 7.75"]
for item in inventory:
print('The store has {1} {0}, each for {2} USD'.format(*item.split(',')))
推荐阅读
- symfony - 如何在 findOneBy 方法中使用请求发布数据
- python - Python - 合并文件/合并文件
- node.js - 如何在 nodejs aws-sdk 模块中设置多个 aws 凭据?
- python - pytest 获取运行时函数测试变量
- sql - 计算在另一个表中找到(或未找到)具有重复值的记录中的某些字段的次数
- angular - NativeScript - 使用 RadForms 时如何检查表单的有效性?
- istio - Istio 特使代理记录丢失的文件
- javascript - 查找 td 文本内容的完全匹配
- python - 用循环中的最新工作表替换数据框中的数据
- axios - Axios 和 RequireJS 集成失败