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

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

相关文章:

python - linux tee 不能与 python 一起使用?

linux - 如何使用 cURL 从 GitHub 下载 tarball?

python - Python 的 List 是如何实现的?

python - 访问对象内存地址

python - 如何在被调用的方法中获取调用者的方法名?

linux - 编写一个shell脚本ssh到远程机器并执行命令

windows - 如何确定特定文件是否在 Windows 中打开?

linux - Linux 中的 'stale file handle' 是什么意思?

python - 如何使用 pandas 读取大型 csv 文件?

python - 打印两个日期之间的所有日期