我有一个看起来像 '%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/