문제

i'm trying to remove question marks from a string in python and I'm wondering what the most efficient way to do it is. I assume searching each word for a ? is not the best way. Just to clarify, I am looking to change this

"What is your name?"

to this

"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

and many more

다른 팁

replace() is simple and efficient:

>>> "What is your name?".replace("?", "")
'What is your name'

In my humble opinion you should look at built-in string.replace() method.

result = "What is your name?".replace('?', '')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top