top of page
  • Pinterest
  • Facebook
  • Instagram

CountLetters

  • Writer: Lim Long Teck
    Lim Long Teck
  • Dec 30, 2021
  • 1 min read

Write a Python program to

- Prompt user to input a sentence

- Count the occurrence of each letter in the input sentence

- Save the result to a dictionary letters where the key is the letter and the value is the occurrence of the key

- Display letters in alphabetical order of the key.


Program:

sentence = input('Enter a sentence: ')

newlist = []

for i in sentence:

i = i.lower()

if i not in newlist and i.isalpha():

newlist.append(i)

numlist = []

for ele in newlist:

count = 0

for alpha in sentence:

if ele == alpha.lower():

count += 1

numlist.append(count)


letters = {}

i = 0

while i <len(newlist):

letters[newlist[i]] = numlist[i]

i += 1


for n in letters:

print('{} : {}'.format(n, letters[n]))


Output:

Enter a sentence: hello

h : 1

e : 1

l : 2

o : 1


Recent Posts

See All
BirthdayParadox

The Birthday Paradox asks the question, “If you asked random people their birthday, how many people do you expect to ask before you find...

 
 
 
ChristmasTree

Write a program to prompt the user for a character and a number, and then print a Christmas tree accordingly. Program: char =...

 
 
 
NumberGuessing

Write a program that simulates a number guessing game. It first generates a random number between 1 and 100. It then prompts user to...

 
 
 

Comments


bottom of page