Find Most Frequent Character

Find character that appears most frequently.

AdvancedPhase 5 - Category 5: Advanced String LogicExample 9 of 14
find-most-frequent-character.py
1# Take string input
2s = input("Enter a string: ")
3
4# Count frequency
5freq = {}
6for char in s:
7 if char != ' ': # Ignore spaces
8 freq[char] = freq.get(char, 0) + 1
9
10# Find most frequent
11if freq:
12 most_frequent = max(freq, key=freq.get)
13 print(f"Most frequent: {most_frequent} (appears {freq[most_frequent]} times)")
14else:
15 print("No characters found")

Output

Enter a string: hello
Most frequent: l (appears 2 times)

What's going on

Count frequency and find maximum.

Key Concepts:

Count frequency of each character
Use max() with key=freq.get
Returns most frequent character