Could you please help me with writing a function to count alphabetic characters. My current output for this code is like this:
It contains 5 alphabetic characters of which 4 ( 80.0 %) are 'h'
My output for this code should be like this: It contains 5 alphabetic characters of which 5 ( 100.0 %) are 'h'. I would like to treat both upper/lowercase letters equally
def count(p):
lows = "abcdefghijklmnopqrstuvwxyz"
ups = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numberOfh = 0
totalChars = 0
for achar in p:
if achar in lows or achar in ups:
totalChars = totalChars + 1
if achar == 'h':
numberOfh = numberOfh + 1
percent_with_h = (numberOfh / totalChars) * 100
print("It contains", totalChars, "alphabetic characters of which", numberOfh, "(", percent_with_h, "%)", "are 'h'.")
p = "Hhhhh"
count(p)