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

说明:

现在有float型值 5

字符型值 a

我原想它们组成一个这样的字符串:5a

但是Python 不允许直接把数字和字符拼接在一起(如果拼在一起就会报标题显示的错误)

示例:

>>> 5+'a'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> '5'+'a'
'5a'

解决办法:

把数字型的字符串,转化为字符型就可以了

>>> '5'+'a'
'5a'
>>> a=5
>>> type(a)
<class 'int'>
>>> a = str(a)
>>> type(a)
<class 'str'>
>>> a+'4'
'54'
原文地址:https://www.cnblogs.com/kaerxifa/p/12882000.html