sample_list当我知道可以容纳 4 件物品时,以下方法有效。
sample_list = ['cat', 'dog', 'bunny', 'pig']
print("Your list of animals are: {}, {}, {} and {}".format(*sample_list))
Run Code Online (Sandbox Code Playgroud)
sample_list如果我不知道运行时将包含的项目数,如何格式化字符串?这意味着我无法在设计时输入适当数量的括号占位符。
我只想用join
sample_list = ['cat', 'dog', 'bunny', 'pig']
printstr = '%s, and %s' % (', '.join(sample_list[:-1]), str(sample_list[-1]))
print("Your list of animals are: %s" % printstr)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2141 次 |
| 最近记录: |