Home>Article>Backend Development> How to count the number of different characters using python

How to count the number of different characters using python

silencement
silencement Original
2019-06-10 15:10:41 15652browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn