Calculating the number of characters that appear in the python string

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/lipachong/article/details/101060651

python number of calculations in all the characters in the string in each occurrence
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))

The number of times specified character string python statistics appear in the code

s = "Count, the number    of spaces."

print (s.count(" "))

x = "I like to program in Python"

print (x.count("i"))

Guess you like

Origin blog.csdn.net/lipachong/article/details/101060651