Python, given a word, generate a list with one letter changing for each letter in alphabet Here is my problem:


  Return the number of times that the string code appears anywhere in the given string, except we'll accept any letter for the d, so cope and cooe count.


I solved it by creating a list with all 26 possible co_e combinations and then used two for loops to iterate through the list and search for matches in the given string.

Is there another way I could have created that list? or is there a way to ignore the third character in word and only search for matching characters 1, 2 and 4?

Here is my solution:

if len(inputstring)&lt;4:
    return 0
wordlist=['coae','cobe','coce','code'...etc]
count=0

for i in wordlist:
    for j in range(len(inputstring)):
        if inputstring[j:j+4] == i:
            count+=1
return count
Just use regular expressions!

import re
# creating regular expression with "any" letter at 3 position
r = re.compile("co[a-z]e")
# and taking all the matches:
matches = r.findall("some of my code, nope to come, cofe and cole, or cope and dome, rope and hope!")
count = len(matches)
print(count)