使用python,出现如下错误TypeError: can only concatenate str (not “int”) to str的解决办法

105 阅读1分钟

本人为python初学者,如有不当或者错误之处,麻烦各位批评指正

使用python语言对str类型和int类型连接时会出现如下错误
错误代码如下

age = 20
print('我的年龄为:'+age)

此时python会报出:TypeError: can only concatenate str (not “int”) to str错误

解决方案:将输入的int类型转换为str类型

age = 20
print('我的年龄为:'+str(age))