Python str()解决类型错误

341次阅读
没有评论

Python

1、说明

将数字与字符串拼接,则会发生类型错误。为了避免这一问题,可以使用str()将数字转换成字符串,然后再处理。

2、实例

>>> age = 18
>>> print('my age is ' + age)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> print('my age is ' + str(age))
my age is 18
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-04-27发表,共计293字。
新手QQ群:570568346,欢迎进群讨论 Python51学习