python - Python:如何用元组中的值替换多次出现的字符串
问题描述
我有一个字符串,例如:
"Hello %s, how are %s, %s"
我需要%s
用元组的元素替换所有出现的("world", "you", 1)
,以便输出为:
Hello world, how are you, 1
解决方案
你可以这样做:
def tupleFormat(string, format):
return string % format
然后你可以做你的例子:
tupleFormat("Hello %s, how are %s, %s", ("world", "you", 1))
您可以完全按照您想要的方式对字符串和元组使用 % 运算符。
推荐阅读
- java - 如何以通用方式进行空检查并返回值
- python - 为 Django 休息使用多个 admin.py 文件?
- sql - 从 CSV NULL 值导入的 PostgreSQL 是文本 - 需要 null
- nginx - Nginx - 部署静态页面
- python - Python 如何安排每一秒的并行脚本?喜欢(cron 工作)
- python - 前瞻和后瞻正则表达式不匹配
- 3d - 如何在这个 Raycaster 引擎中预先计算这个光照层
- python - 如何在 Tkinter 中刷新没有 .mainloop() 函数的窗口?
- r - 如何以“tidyverse”的方式在 R 中按名称重新编码几列?
- macos - https 在 macos 上使用 openssl CA 证书作为受信任