[英]Question about String operations in Python
我是 Python 的新手,正在練習文件操作。 我寫了這個程序:
myfile = open('test3.txt', 'w+')
myfile.writelines(['Doctor', 'Subramanian', 'Swamy', 'Virat', 'Hindustan', 'Sangam'])
它輸出以下內容: DoctorSubramanianSwamyViratHindustanSangam
。
如何在最終 output 的列表項之間添加空格,以便最終 output 是Doctor Subramanian Swamy Virat Hindustan Sangam
?
根據我從您的問題中了解到的情況,您希望在最終 output 中的列表元素之間添加空格。 一種可能的解決方案是:
myfile = open('test3.txt', 'w+')
list = ['Doctor', 'Subramanian', 'Swamy', 'Virat', 'Hindustan', 'Sangam']
for l in list:
myfile.write(l+' ')
特別是,這一行myfile.write(l+' ')
將在寫入每個元素后添加一個空格。
您可以嘗試使用 same.strip() 方法剝離它嗎?
value = "'"
list1 = []
for item in list2:
list1.append(item.strip("{0}".format(value)))
試試看,讓我知道
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.