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

Python counts the number of different characters

angryTom
Release: 2020-02-07 10:22:05
Original
17373 people have browsed it

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)
Copy after login

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))
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template