Python TypeError:在字符串格式化过程中并非所有参数都已转换

Python TypeError:在字符串格式化过程中并非所有参数都已转换,python,python-3.x,string,printing,typeerror,Python,Python 3.x,String,Printing,Typeerror,上面的语句给出了一个错误“TypeError:在字符串格式化过程中并非所有参数都转换” 任何帮助都将不胜感激。+的优先级低于%,因此,在您的代码中,Python尝试评估“无谷物的有机狗粮”。%'hurry',这没有意义,因为该格式字符串不包含%s部分 删除字符串文字之间的+: print("The mangy, scrawny stray dog %s gobbled down" + "the grain-free, organic dog food."

上面的语句给出了一个错误“TypeError:在字符串格式化过程中并非所有参数都转换”


任何帮助都将不胜感激。

+
的优先级低于
%
,因此,在您的代码中,Python尝试评估
“无谷物的有机狗粮”。%'hurry'
,这没有意义,因为该格式字符串不包含
%s
部分


删除字符串文字之间的
+

print("The mangy, scrawny stray dog %s gobbled down" +
"the grain-free, organic dog food." %'hurriedly')
相邻的字符串文字无论如何都是连接在一起的,因此您不需要
+

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

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

删除字符串文本之间的
+
<代码>+的优先级低于
%
,并且相邻的字符串文字仍然是串联的。
adverb = 'hurriedly'
print(f"The mangy, scrawny stray dog {adverb} gobbled down "
      "the grain-free, organic dog food.")