如何在 Python 中使用多个字符串参数

2024-03-13

我想使用 % s 将两个参数传递给我的字符串。

我尝试了这个,但没有成功:

title = "im %s with %s"
title % "programming" % "python"

它给出了这个错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string

你有好主意吗? 谢谢


正确的语法是:

title = "im %s with %s"
title % ("programming", "python")

The %运算符有两个操作数:

  1. 格式字符串位于左侧;
  2. 包含所有参数的元组位于右侧(如果只有一个参数,则它可以是标量)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Python 中使用多个字符串参数 的相关文章

随机推荐