Count individual occurance of character in Python

0 votes
I want to count the number of times a character occurs in a String. Can someone help me with the logic?
Nov 19, 2018 in Python by Jino
• 5,560 points
49 views

1 answer to this question.

0 votes

Check this code it works fine.

from collections import Counter
inp_string=input("Enter your string\n")
lis=list(inp_string)
length=len(inp_string)
cnt = Counter()
for i in lis:
    ls= set(i)
    for l in ls:
        cnt[l] +=1
print (cnt)
answered Nov 19, 2018 by Nabarupa

Related Questions In Python

+1 vote
4 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
1,444 views
0 votes
2 answers

Finding the index of a character in python string

You can use word.find('o') as well to ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
119 views
0 votes
1 answer

How to print first character of each word in upper case of a string in Python

You can use split() using space as ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
2,380 views
0 votes
2 answers

Python program to print occurrence of character in string

Try the below code inputString = input("Enter a ...READ MORE

answered Feb 26 in Python by Anubhav
• 160 points
1,114 views
+4 votes
6 answers
0 votes
1 answer

Removing surrounding whitespace

Try the strip() function:  s = s.strip() If you ...READ MORE

answered May 4, 2018 in Python by Nietzsche's daemon
• 4,260 points
47 views
0 votes
1 answer

How can I get the count of a list in Python?

len() >>> mylist=[] >>> print len(mylist) 0 READ MORE

answered Dec 12, 2018 in Python by charlie_brown
• 7,720 points
90 views
+4 votes
6 answers

Use of "continue" in python

The break statement is used to "break" ...READ MORE

answered Jul 16, 2018 in Python by Omkar
• 67,660 points
138 views