I’m trying to create a function that takes a string with letters, numbers, and other characters, and then returns a string with only numbers and question marks in it. When I run the following function with the input arrb6???4xxbl5???eee5, it returns the whole string and not just numbers. What can I do to improve my code?
def questionmarkstry(string):
list_string = [x for x in string]
liststringnew = []
for letter in list_string:
if string == "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9" or "0" or "?":
liststringnew.append(letter)
elif string != "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9" or "0" or "?":
pass
return liststringnew
Here is an updated version:
def questionmarkstry(string):
list_string = [x for x in string]
liststringnew = []
for l in list_string:
if l == "1" or l == "2" or l == "3" or l == "4" or l == "5" or l == "6" or l == "7" or l == "8" or l == "9" or l == "0" or l =="?":
liststringnew.append(l)
else:
pass
return(liststringnew)