版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
python计算所有字符在字符串中各自出现的次数
1
from collections import Counter
str='1212jisajikodsakdokoakso'
counts=Counter(str)
print(counts)
2
message='Thdsaa'
count={}
for character in message:
count.setdefault(character,0)
count[character]=count[character]+1
print(count)
#计算出现次数最多的字符
for key,value in count.items():
if(value == max(count.values())):
print(key,value)
3
str = "I like to program in Python"
for y in str :
print (y,str.count(y))
python统计字符串中指定字符出现的次数的代码
s = "Count, the number of spaces."
print (s.count(" "))
x = "I like to program in Python"
print (x.count("i"))