python TypeError: unsupported operand type(s) for +: 'int' and 'str'

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34579060/article/details/87067719

python 中 打印时,不支持 2 + "字符串"

print(2+"test")

会抛出如下异常 :

      python TypeError: unsupported operand type(s) for +: 'int' and 'str'

“+”  在python中有两个作用

       一个是数学运算符,是用来在整型、浮点型等数学之间进行加法操作的。

      另一个是用来进行字符串连接的。所以当你的“+”出现在即有数学运算和字符连接的情况下,计算机根本不知道哪两个是要进行字符串连接,哪两个之间要进行数学运算。

 如果想要将 int 和 string 拼接在一起可以使用:

 print(str(2) + "a")

猜你喜欢

转载自blog.csdn.net/qq_34579060/article/details/87067719