python字符串拼接TypeError: cannot concatenate 'str' and 'int' objects

python字符串拼接TypeError: cannot concatenate 'str' and 'int' objects,第1张

python字符串拼接TypeError: cannot concatenate 'str' and 'int' objects

        在对python字符串进行拼接的时候,报错:python字符串拼接时如果变量的类型不同就会报错TypeError: cannot concatenate 'str' and 'int' objects。需要对字符串中如有整型之类的变量进行转换类型。

        如:对变量进行转字符串处理,使用函数str()

a = '你的编号为:'
b = 123
#拼接字符串
#print type(b)
str = a+str(b)
print str

如果不加str()对整型进行处理就会报上面的错误。

    python str和int互转:

int转string:

int(string)

string转int:

str(number)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3005114.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-27
下一篇 2022-09-27

发表评论

登录后才能评论

评论列表(0条)

保存