统计一个子字符在另外一个字符串中出现的次数

#coding = utf-8
'''
Created on 2015年5月30日 
author: likui
'''

def findstr(desStr,subStr):
    '该函数统计一个子字符在另外一个字符串中出现的次数'
    des_len=len(desStr)
    sub_len=len(subStr)
    des2list=list(desStr)
    temp=[]
    
    if subStr not in desStr:
        print('关键字不存在')
    else:
        for each1 in range(des_len):
            des_temp=des2list[each1:each1+sub_len]
            temp_str=''
            for each2 in des_temp:
                temp_str+=each2
            temp.append(temp_str)
    count=temp.count(subStr)
    print('字符出现了%d次'%count)
    
findstr('你的世界你做主你的世你做主你的界你做主你的界你做主', '你的世界')


原文地址:https://www.cnblogs.com/lkpp/p/7400056.html