how to find most repeated word in a string in python
from collections import Counter # Example phrase phrase = "John is the son of John second. Second son of John second is William second." split_phrase = phrase.split() # create counter object Counter = Counter(split_phrase) most_occured_words = Counter.most_common(4) print(most_occured_words)
most repeated character in a string python
### Has No Modules ### # The string string = input("What 'thing' do you want the most repeated character of? Just type it!: ") # Setting an empty dictionary (Key = Character + Value = Frequency) character_frequency = {} for character in string: if character in character_frequency: character_frequency[character] += 1 else: character_frequency[character] = 1 Values = sorted([kv[1] for kv in character_frequency.items()], reverse=True) for k,v in character_frequency.items(): if v == Values[0]: print("'" + k + "'", "is the most repeated character in '" + string + "'.")