格式化字符串给定占位符

我必须用列表中的变量替换某些占位符,但是我不确定如何重新格式化占位符。

warning = 'I don\'t recognize the name {0}. Did you mean {1}?'

上面的代码行是我需要重新格式化的。任何帮助将不胜感激。



郎朗坤
浏览 230回答 1
1回答

慕桂英3389331

这是默认的 Python 格式字符串,它可以与format.>>> warning = 'I don\'t recognize the name {0}. Did you mean {1}?'>>> warning.format("Meliodus123", "format")"I don't recognize the name Meliodus123. Did you mean format?"中的数字{0}表示传递给 的参数位置format。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python