TypeError : not all arguments converted during string formatting

TypeError : not all arguments converted during string formatting

print("The mangy, scrawny stray dog %s gobbled down" +
"the grain-free, organic dog food." %'hurriedly')

上面的语句给出了错误“TypeError:在字符串格式化期间并非所有参数都已转换”。

我们将不胜感激。

+ 的优先级低于 %,因此对于您的代码,Python 尝试计算 "the grain-free, organic dog food." %'hurriedly',这没有意义,因为该格式字符串确实不包含 %s 部分。

删除字符串文字之间的 +

print("The mangy, scrawny stray dog %s gobbled down "
      "the grain-free, organic dog food." %'hurriedly')

无论如何,相邻的字符串文字都是连接在一起的,因此您不需要 +.

或者,如果您使用的是 Python 3.6 或更高版本,请使用 f 字符串:

adverb = 'hurriedly'
print(f"The mangy, scrawny stray dog {adverb} gobbled down "
      "the grain-free, organic dog food.")