number of occurrence counting in a string

0 votes
Write a function called number_of_occurrences that takes in a string and outputs the the number of occurrences that a character shows up in a dictionary. This dictionary should be ordered by the number of occurrences in descending order. You can ignore spacing, capitalization, and punctuation.
May 16, 2020 in Python by Anny
• 120 points
2,381 views

Hey, @Anny,

You can try this:

from collections import Counter 

# initializing string 

test_str = "Pythonforall"

# using collections.Counter() to get  

# count of each element in string 

res = Counter(test_str) 

# printing result  

print ("Count of all characters in Pythonforall is :\n "

                                           +  str(res))

Count of all characters in Pythonforall is :
Counter({'o': 2, 'l': 2, 'a': 1, 'f' : 1, 'h': 1, 'n' : 1, 'p': 1, 'r': 1, 't': 1, 'y' : 1})

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
3 answers

Count the number occurrences of a character in a string

The string count() method returns the number of occurrences of ...READ MORE

answered Dec 28, 2020 in Python by Carlos
14,090 views
0 votes
0 answers

How can I find the first occurrence of a sub-string in a python string?

I'd like to locate the first index ...READ MORE

Nov 24, 2022 in Python by sarit
• 1,830 points
570 views
0 votes
2 answers

How to calculate square root of a number in python?

calculate square root in python >>> import math ...READ MORE

answered Apr 2, 2019 in Python by anonymous
5,850 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
1,551 views
+1 vote
2 answers

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

class Solution:     def firstAlphabet(self, s):             self.s=s              k=''              k=k+s[0]              for i in range(len(s)):                     if ...READ MORE

answered Oct 28, 2020 in Python by Anurag
12,374 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,460 points
1,370 views
–1 vote
2 answers

How to find the size of a string in Python?

following way to find length of string  x ...READ MORE

answered Mar 29, 2019 in Python by rajesh
• 1,270 points
2,099 views
+5 votes
1 answer

Print the odd and even factors of a number in Python

x = int(input("Enter any number \n")) print("The factors ...READ MORE

answered Nov 19, 2018 in Python by Nabarupa
17,550 views
–1 vote
2 answers
0 votes
3 answers

Python program to print occurrence of character in string

Try below code string = input("Enter a string: ...READ MORE

answered May 28, 2020 in Python by Anubhav
• 300 points
30,748 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP