Logic Building
# Take string input
s = input("Enter a string: ")
# Count characters
freq = {}
for char in s:
freq[char] = freq.get(char, 0) + 1
# Display
for char, count in sorted(freq.items()):
print(f"{char}: {count}")Output
Enter a string: hello e: 1 h: 1 l: 2 o: 1
Use dictionary to count frequency.
Key Concepts:
- Use dictionary to track counts
- freq.get(char, 0) returns count or 0
- Increment for each occurrence