Find most occurring characters in a string using Python
-
Sample code snippet to find the most repeated characters or words in a given string excluding spaces. Also display the count of the most repeated items. Implemented using built-in string methods in Python.
-
Find most repeated character
This sample finds the most occurring character or characters in a given string and its count also. Here we are not considering case difference. First convert the string to lower and replace white spaces.
Next find the unique characters in the string by converting it to a set (sets holds unique items only). Finally loop through each character and find the count.
Copiedteststr = 'This is a test string' #Remove white spaces teststr = teststr.lower().replace(" ", "") #Identify unique characters charsinstr = list(set(teststr.strip())) #Find most repeated character mostoccurringchar = [] count = 0 for c in charsinstr: charcount = teststr.count(c) if (charcount > count): count = charcount mostoccurringchar = [] mostoccurringchar.append(c) elif (charcount == count): mostoccurringchar.append(c) print("Most occurring characters are " + str(mostoccurringchar)) print("It's count is " + str(count))
Most occurring characters are ['s', 't']
It's count is 4 -
Find most repeated words
Here, we will first convert the string to a list of words using split function. Then loop through each word in the string and find its count. Store the words with most count in a list variable and count also.
Copiedteststr = 'This is a test string. This test the most used words here. ' #Remove dots teststr = teststr.lower().replace(".", "") #Convert string to list wordsinstr = teststr.split() #Find most repeated character mostoccurringword = [] count = 0 for word in wordsinstr: wordcount = wordsinstr.count(word) if (wordcount > count): count = wordcount mostoccurringword = [] mostoccurringword.append(word) elif (wordcount == count) and (not(word in mostoccurringword)): mostoccurringword.append(word) print("Most occurring words are " + str(mostoccurringword)) print("It's count is " + str(count))
Most occurring words are ['this', 'test']
It's count is 2