CBSE Class 11 Computer Science Question 102 of 112

Dictionaries — Question 2

Back to all questions
2
Question

Question 2

Write a program to count the number of times a character appears in a given string.

Solution
str = input("Enter the string: ")
ch = input("Enter the character to count: ");
c = str.count(ch)

print("Count of character",ch,"in",str,"is :", c)
Output
Enter the string: appoggiatura
Enter the character to count: a
Count of character a in appoggiatura is : 3
Answer