Thursday 15 February 2024

Class 11 CS Practical - 47

 47 - WAP in Python to input a string and display the frequency of each character using dictionary


S=input("Enter a string: ")

F={}

for i in S:

    if i in F:

        F[i]+=1

    else:

        F[i]=1

print("Frequency of characters in the given string: ")

for i, j in F.items():

    print(i,":",j)



Output:

Enter a string: S P SHARMA

Frequency of characters in the given string:

S : 2

  : 2

P : 1

H : 1

A : 2

R : 1

M : 1


No comments:

Post a Comment