1

我正在尝试从 python 中的字符串中删除问号,我想知道最有效的方法是什么。我假设在每个单词中搜索一个 ? 不是最好的方法。只是为了澄清,我希望改变这一点

"What is your name?"

对此

"what is your name"
4

3 回答 3

5
"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

还有很多

于 2013-09-21T19:35:40.613 回答
3

replace()简单高效:

>>> "What is your name?".replace("?", "")
'What is your name'
于 2013-09-21T19:35:47.977 回答
2

以我的拙见,您应该查看内置的 string.replace() 方法。

result = "What is your name?".replace('?', '')
于 2013-09-21T19:38:47.670 回答