記錄日常學習遇到的問題
1.串列中的元素為字串時
l1=['a','p','p','l','e']
l2=''.join(l1)
print(l2)
[outs]:
apple
2.串列中的元素為數字時,不能用join直接拼接串列中的元素
l1=[2,0,2,1]
l2=''.join(l1)
print(l2)
[outs]:
TypeError: sequence item 0: expected str instance, int found
正確的方式為將串列中的數字遍歷后轉化為str型別再拼接,具體操作如下
l1=[2,0,2,1]
l2=''.join(str(num) for num in l1)
print(l2)
[outs]:
2021
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/378656.html
標籤:其他
上一篇:Plt_tk
