我有一個元組列表:x = [("initial deposit", 1000.00), ("restaurant and more foo", -15.89)]如何通過將每個單獨的元組轉換為字符串并將元組的單個字符串相加來獲得單個字符串?我的意思是,我希望打印以下內容,但它必須是單個字符串。所以,我的命令是這樣的:print(single_string)并將打印以下內容:initial deposit 1000.00
restaurant and more foo -15.89我該怎么做?
1 回答

慕姐8265434
TA貢獻1813條經驗 獲得超2個贊
這與列表中的元組數呈線性關系:x
x = [("initial deposit", 1000.00), ("restaurant and more foo", -15.89)]
s = ''
for e in x:
s += e[0] + '\t' + str(e[1]) + '\n'
print(s)
輸出:
initial deposit 1000.0
restaurant and more foo -15.89
請注意制表符和行尾字符。\t\n
- 1 回答
- 0 關注
- 137 瀏覽
添加回答
舉報
0/150
提交
取消