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"))