Spa*_*xxy 5 python string concatenation format-string
我目前正在编写一个进行频率分析的短程序.然而,有一条线困扰着我:
"{0[0]} | " + "[]" * num_occurrences + " Total: {0[1]!s}"
Run Code Online (Sandbox Code Playgroud)
在Python中有没有一种方法可以重复某些字符而不需要连接(最好是在格式字符串中)?我不觉得我是以最恐怖的方式做这件事.
Dav*_*ver 15
重复一个字符或字符串的最好方法是乘以它:
>>> "a" * 3
'aaa'
>>> '123' * 3
'123123123'
Run Code Online (Sandbox Code Playgroud)
对于你的例子,我可能会使用:
>>> "{0[0]} | {1} Total: {0[1]!s}".format(foo, "[]" * num_occurrences)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11913 次 |
| 最近记录: |