說明:
現在有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'