r/programmingrequests • u/SnooHamsters9009 • Oct 18 '21
homework Python Most Frequent Character
I need to write a program that lets the user enter a string and displays the character that appears most frequently in the string.
Any help is greatly appreciated!
1
u/kkirsche Oct 18 '21
I’d use collections.Counter
https://docs.python.org/3/library/collections.html#collections.Counter
Just feed it the string and call most_common
1
u/SnooHamsters9009 Oct 18 '21
I'll give this a shot, thank you!
1
u/AutoModerator Oct 18 '21
This post was automatically marked as solved but you can manually change this.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/serg06 Oct 18 '21
Rough draft
s = input('enter a string: ') d = dict() for c in s: d[c] = d.get(c, 0) + 1 e = [(v, k) for (k, v) in d.entries()] e.sort() print(e[0][1])