How to print histogram of frequencies of characters in string with Python

2 Answers

0 votes
s = 'Python is an interpreted high-level general-purpose programming language.'

Dict=dict()
 
for ch in s:
    if ch not in Dict:
        Dict[ch] = 1
    else:
        Dict[ch] = Dict[ch] + 1
 
for key, val in Dict.items():
    print(key, '*' * val)
     
     
     
 
'''
run:
 
P *
y *
t ***
h ***
o ***
n ******
  *******
i ****
s **
a *****
e *********
r ******
p ****
d *
g ******
- **
l ****
v *
u **
m **
. *
 
'''

 



answered Oct 3, 2021 by avibootz
edited Oct 3, 2021 by avibootz
0 votes
s = 'Python is an interpreted high-level general-purpose programming language.'

frequencies = {}

for ch in s:
    frequencies[ch] = frequencies.get(ch, 0) + 1
 
for item in frequencies.items():
    stars = ""
    for i in range(item[1]):
        stars += "*"
        
    print(item[0] + " " + stars)
     
     
     
     
'''
run:
 
P *
y *
t ***
h ***
o ***
n ******
  *******
i ****
s **
a *****
e *********
r ******
p ****
d *
g ******
- **
l ****
v *
u **
m **
. *
 
'''

 



answered Oct 3, 2021 by avibootz
...