我正在尝试从 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'
以我的拙见,您应该查看内置的 string.replace() 方法。
result = "What is your name?".replace('?', '')