我對 python 比較陌生,我正在嘗試與串列連接。這是我最好的嘗試,但顯然它不起作用。有人知道這樣做的方法嗎?謝謝
present = ["Jake", "Jimmy", "Karen"]
print("The people present are " present)
uj5u.com熱心網友回復:
您需要先將串列轉換為字串,然后才能使用 運算子進行字串連接。您可以使用該str.join()方法來完成此操作。
present = ["Jake", "Jimmy", "Karen"]
print("The people present are " ", ".join(present))
這將為串列中的每個專案創建一個字串,并使用該字串', '作為每個元素之間的分隔符。
在此處查看 w3 學校示例https://www.w3schools.com/python/ref_string_join.asp
或者官方 python 檔案https://docs.python.org/3/library/stdtypes.html#str.join
uj5u.com熱心網友回復:
你可能想要 str.join
>>> some_list = ['Jack', 'Jill', 'Hill']
>>> print("The story centrally features the characters: " ", ".join(some_list))
The story centrally features the characters: Jack, Jill, Hill
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/374978.html
上一篇:在特定行的特定列上向前填充
