Python笔记-TypeError: can only concatenate str (not "int") to str

错误提示:

TypeError: can only concatenate str (not "int") to str

解释:字符串只能和字符串连接

 

错误代码:

 

name = '小明'
year = 14
print("他叫"+name+",他"+year+"岁了。")

 

 

 

修改错误:在year前面加上str(),将int类型转换为字符串类型。

 

name = '小明'
year = 14
print("他叫"+name+",他"+str(year)+"岁了。")

 

 

 

 

(๑╹ᆺ╹)  感谢观看,希望对你有帮助!

 

posted @ 2022-10-21 20:28  Mr_宋先生  阅读(222)  评论(0编辑  收藏  举报