首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python中的输出格式设置:用同一变量替换多个%s

Python中的输出格式设置:用同一变量替换多个%s
EN

Stack Overflow用户
提问于 2011-08-08 21:36:50
回答 9查看 51.2K关注 0票数 23

我正在尝试维护/更新/重写/修复一些看起来有点像这样的Python:

代码语言:javascript
运行
复制
variable = """My name is %s and it has been %s since I was born.
              My parents decided to call me %s because they thought %s was a nice name.
              %s is the same as %s.""" % (name, name, name, name, name, name)

脚本中到处都是这样的小片段,我想知道是不是有更简单的(更Pythonic的?)写这段代码的方式。我发现了一个这样的实例,它将相同的变量替换了大约30次,感觉很难看。

在我看来,绕过丑陋的唯一方法是把它分成许多小块吗?

代码语言:javascript
运行
复制
variable = """My name is %s and it has been %s since I was born.""" % (name, name)
variable += """My parents decided to call me %s because they thought %s was a nice name.""" % (name, name)
variable += """%s is the same as %s.""" % (name, name)
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6982949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档