python使用count(s,f)函数

puthon使用count(s,f)函数统计字符串中数字,大写字母,小写字母和其他字符的数目


 
 
def count(s,l=0):
    countdict = {
             0:0,
             1:0,
             2:0,
             3:0,
         }
    for i in s:
        if i.isdecimal():#数字
             countdict[0] += 1
        elif i.isupper():#大写字母
            countdict[1]  += 1
        elif i.islower():#小写字母
            countdict[2]  += 1
        else:#其他
            countdict[3]  += 1
    return countdict[l]
            
 
for i in range(4):
    c=count("sw3A2SWj9k+86*", l= i)
    print(c)
    

count(sub,[start, end])str类型方法
统计在给定字符串中从start到end的地方出现了几次sub,其中start和end可以省略,省略的情况下默认遍历整个str
sub = 你要找的字符串,所以数字也好字符也好都得是str类型
start,检索开始的索引
end,检索结束的索引
a = "123"
a.count("a") 就会 输出1