我正在尝试从python中的字符串中删除问号,我想知道最有效的方法是什么。我假设搜索每个单词?不是最好的方法。只是为了澄清,我希望改变这个
"What is your name?"
到这个
"what is your name"
答案 0 :(得分:3)
"What is your name?".replace("?","") #this is the most clear
#or
filter(lambda x:x!= "?","What is your name?")
#or
"".join(x for x in "What is your name?" if x != "?")
#or
"What is your name?".translate(None,"?") #this is my favorite
还有更多
答案 1 :(得分:2)
replace()
简单而有效:
>>> "What is your name?".replace("?", "")
'What is your name'
答案 2 :(得分:2)
在我看来,你应该看一下内置的string.replace()方法。
result = "What is your name?".replace('?', '')