gpt4 book ai didi

python - 在 Python 中使用多个参数进行字符串格式化(例如 '%s ... %s' )

转载 作者:IT老高 更新时间:2023-10-28 12:25:24 25 4
gpt4 key购买 nike

我有一个看起来像 '%s in %s' 的字符串,我想知道如何分隔参数,以便它们是两个不同的 %s。我从 Java 中想到了这个:

'%s in %s' % unicode(self.author),  unicode(self.publication)

但这不起作用,那么它在 Python 中的外观如何?

最佳答案

Mark Cidade 的回答是正确的 - 您需要提供一个元组。

但是,从 Python 2.6 开始,您可以使用 format而不是 %:

'{0} in {1}'.format(unicode(self.author,'utf-8'),  unicode(self.publication,'utf-8'))

不再鼓励使用 % 格式化字符串。

This method of string formatting is the new standard in Python 3.0, and should be preferred to the % formatting described in String Formatting Operations in new code.

关于python - 在 Python 中使用多个参数进行字符串格式化(例如 '%s ... %s' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3395138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com