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'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM