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

Answer :-

string = input("Enter String :- ")
dic =  { }
for i in string :
      dic [ i ] = string.count( i )

print(dic)

Output:- 

Enter String :- Pathwalla
{'P': 1, 'a': 3, 't': 1, 'h': 1, 'w': 1, 'l': 2}
>>>

Enter String :- Mississippi
{'M': 1, 'i': 4, 's': 4, 'p': 2}
>>>

11 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post