Home  >  Article  >  Backend Development  >  Python counts the number of different characters

Python counts the number of different characters

angryTom
angryTomOriginal
2020-02-07 10:22:0517342browse

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:

Python counts the number of different characters

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!

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