Day 12 of 100 Days of Python
Practising Dictionary
Today is Day 12. I practised more programs based on the dictionary.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Write a program that looks at a string, counts the characters, and tells you which character appeared the most times total.
st = "Thisisastring"
a = {}
max = 0
most_frequent = ""
for ch in st:
if ch in a:
a[ch] += 1
else:
a[ch] = 1
print(a)
for ch in st:
if a[ch]>max:
max = a[ch]
most_frequent = ch
print(f"Most frequent: {most_frequent} \nNo of times appeared: {max}")
Output:
1
2
3
{'T': 1, 'h': 1, 'i': 3, 's': 3, 'a': 1, 't': 1, 'r': 1, 'n': 1, 'g': 1}
Most frequent: i
No of times appeared: 3
This post is licensed under CC BY 4.0 by the author.