Home>Article>Backend Development> How to count the number of different characters using python
How to count the number of times different characters appear in a string?
Then use the powerful python to solve it!
Example
#统计不同字符的次数 str_1 = input("请输入一串字符:") result = {} for i in str_1: result[i] = str_1.count(i) print(result)
Output result
请输入一串字符:afbvssevsbrfnvb {'a': 1, 'f': 2, 'b': 3, 'v': 3, 's': 3, 'e': 1, 'r': 1, 'n': 1}
Analysis: First, we use the input function to obtain the user's input, and at the same time create an empty dictionary result for storage Statistical results; secondly, use a for loop to traverse the user's input, add the traversal results and statistical results to the result, print the result, and realize the statistics of the number of characters.
The above is the detailed content of How to count the number of different characters using python. For more information, please follow other related articles on the PHP Chinese website!