【发布时间】:2013-10-25 04:50:29
【问题描述】:
创建一个 Python 程序,将字符串转换为列表,使用循环删除任何标点符号,然后将列表转换回字符串并打印不带标点符号的句子。
punctuation=['(', ')', '?', ':', ';', ',', '.', '!', '/', '"', "'"]
str=input("Type in a line of text: ")
alist=[]
alist.extend(str)
print(alist)
#Use loop to remove any punctuation (that appears on the punctuation list) from the list
print(''.join(alist))
这是我目前所拥有的。我尝试使用类似 alist.remove(punctuation) 的东西,但我收到一个错误,比如 list.remove(x): x not in list。一开始我没有正确阅读这个问题,并意识到我需要通过使用循环来做到这一点,所以我将其添加为评论,现在我被卡住了。但是,我成功地将它从列表转换回字符串。
【问题讨论】:
-
只需使用
replace或strip。
标签: python list loops punctuation