How to find the number of occurrences (frequency) of each character in a string with Python

1 Answer

0 votes
def count_char(s):
  for i in range(len(s)):
    if len(s) == 0:
      break;
    ch = s[0]
    if ch == ' ' or ch == '\t':
      continue
    print(ch + " - ", s.count(ch))
    s = s.replace(ch, '').strip()

count_char('Python is an interpreted high-level general-purpose programming language')





'''
run:

P -  1
y -  1
t -  3
h -  3
o -  3
n -  6
i -  4
s -  2
a -  5
e -  9
r -  6
p -  4
d -  1
g -  6
- -  2
l -  4
v -  1
u -  2
m -  2

'''

 



answered Jun 5, 2021 by avibootz
...