【发布时间】:2014-08-24 21:45:51
【问题描述】:
我发现用我当前使用的语法格式化一个字符串需要花费很多时间和精力:
myList=['one','two','three']
myString='The number %s is larger than %s but smaller than %s.'%(myList[1],myList[0],myList[2])
结果:
"The number two is larger than one but smaller than three"
很奇怪,但每次我到达% 键盘键后跟s 时,我都觉得有点被打断了......
我想知道是否有其他方法可以实现类似的字符串格式。请发布一些示例。
【问题讨论】:
-
您的意思是您的打字流程被打断了?
-
这不是重复的。格式只是处理字符串的众多可能方法之一。连接等其他选项是此问题的一个选项。或者可能基于
string.join
标签: python string string-formatting