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"))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章