2024-06-01 04:06:15 发布
网友
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部分
+
%
"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.")
+
的优先级低于%
,因此在代码中,Python尝试计算"the grain-free, organic dog food." %'hurriedly'
,这没有意义,因为格式字符串不包含%s
部分删除字符串文字之间的
+
:相邻的字符串文字无论如何都是连接在一起的,因此不需要
+
或者,如果您使用的是Python 3.6或更高版本,请使用f字符串:
相关问题 更多 >
编程相关推荐