python中统计字符串多个字符同时出现的次数

 

001、

>>> str1 = "abcdabaewr"                ## 测试字符串
>>> import re
>>> len(re.findall("a", str1))         ## 统计a出现的次数
3
>>> len(re.findall("b", str1))         ## 统计b出现的次数
2
>>> len(re.findall("ab", str1))        ## 统计ab出现的次数
2
>>> len(re.findall("[ab]", str1))      ## 统计字符a和字符b同时出现的次数
5
>>> str1.count("a")                    
3
>>> str1.count("b")
2
>>> str1.count("ab")
2

 

posted @ 2022-08-11 19:35  小鲨鱼2018  阅读(331)  评论(0编辑  收藏  举报