Python 返回单词在缺少字母的字符串中出现的次数

Python 返回单词在缺少字母的字符串中出现的次数,python,string,Python,String,假设我有一个字符串“aaatapoaaatacoaaa”,我想找出出现“taco”的次数,但是如果字母“c”被其他字符替换,我仍然想增加总次数,所以像“tapo”或“taoo”这样的单词仍然有效。如果没有任何内置的字符串搜索方法,如str.find,我将如何做到这一点 我只到目前为止: def count_taco(a): amount = 0 for letter in a: count_taco("aaatapoaaatacoaaa") 这应该起作用: >>&

假设我有一个字符串
“aaatapoaaatacoaaa”
,我想找出出现“taco”的次数,但是如果字母
“c”
被其他字符替换,我仍然想增加总次数,所以像
“tapo”
“taoo”
这样的单词仍然有效。如果没有任何内置的字符串搜索方法,如
str.find
,我将如何做到这一点

我只到目前为止:

def count_taco(a):
    amount = 0
    for letter in a:

count_taco("aaatapoaaatacoaaa")
这应该起作用:

>>> def count_taco(a):

        amount = 0
        chars = 'abcdefghijklmnopqrstuvwxyz'
        for c in chars:
            s = 'ta{}o'.format(c)
            if s in a:
                amount += a.count(s)

        return amount

>>> my_test = 'aaatapoaaatacotaooaaatacotapo'
>>> count_taco(my_test)
5 #tapo - taco - taoo - taco - tapo

re.search(r'ta\wo')
@BobDylan我建议
len(re.findall('ta.o',s))
。如果没有内置的字符串搜索方法,请使用并修改它,使第三个字母可以是任何东西。这似乎对我帮助不大:
“tacotaco”
?你似乎认为任何“ta.o”只能出现一次。@timgeb。。谢谢你的提醒…:)。。当我写这个答案时,我正在思考另一个问题P