我試圖從python中的字符串中移除問號,我想知道最有效的方法是什麼。我假設搜索每個單詞?不是最好的方法。只是爲了澄清,我期待改變這種從字符串中移除問號
"What is your name?"
這個
"what is your name"
我試圖從python中的字符串中移除問號,我想知道最有效的方法是什麼。我假設搜索每個單詞?不是最好的方法。只是爲了澄清,我期待改變這種從字符串中移除問號
"What is your name?"
這個
"what is your name"
"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
,還有更多
replace()
是簡單而有效:
>>> "What is your name?".replace("?", "")
'What is your name'
在我謙卑的意見y你應該看看內置的string.replace()方法。
result = "What is your name?".replace('?', '')
的可能重複的[正則表達式,以除去從字符串換行符](http://stackoverflow.com/questions/18935859/regex-to-remove-newline-character-from-string) – Mark