Home > Article > Backend Development > Python counts the number of different characters
python counts the number of different characters
First use input to get the input data and store it in the str parameter
Then use a for loop for each character of str. Within the loop, use str.count() to get the number of words that the character appears in and store it in a dictionary.
The final output dictionary is Can.
The code is as follows:
str = input("请输入一串字符:") resoult = {} for i in str: resoult[i] = str.count(i) print(resoult)
The effect is as follows:
Note:
Python count() method is used for statistics The number of occurrences of a character in a string. Optional parameters are the start and end positions of the string search.
Grammar:
str.count(sub, start= 0,end=len(string))
Many python training videos, all on the python learning network, welcome to learn online!
The above is the detailed content of Python counts the number of different characters. For more information, please follow other related articles on the PHP Chinese website!