-1

read the text from file.txt and count the number of each alphabets in that and out put should be like a=2 ( if there are 2 a's) and b=6 (if there are 6 b's) so far i have done this much, this prints every alphabet but i want to print alphabets which exists only.

f= open('cipher.txt')
word= " ".join(line.strip() for line in f)
word=word.lower()
alpha="abcdefghijklmnopqrstuvwxyz"
alpha=list(alpha)
for i in alpha:
  print(i+"="+str(word.count(i)))

But if any alphabet which is 0 times used i dont want to print that,, how to fix this? Help PLEASE

1 Answers1

0
import letters
s = 'hello world'
{a:s.count(a) for a in string.letters if s.count(a) != 0}
OUTPUT:
{'d': 1, 'e': 1, 'h': 1, 'l': 3, 'o': 2, 'r': 1, 'w': 1}
Denis
  • 7,127
  • 8
  • 37
  • 58