python计算字符在字符串中出现的次数

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

你可能感兴趣的:(经验记录)