String

Python code: Find the frequency of each character in the string

Python code: Find the frequency of each character in the string

In this article, we will discuss the concept of the Python code: Find the frequency of each character in the string

In this post, we are going to learn how to  find the frequency of  the each character of the given string in Python programming language

Find the frequency of each character in the string

Program to find the frequency of  the each characters in the string

find the frequency of  the each character in the string -method 1

The program allows the user to enter a String and then it finds the frequency of the each characters in the given string

program 1

string=input("Enter the string ")
freq=[None]*len(string)

for i in range(0,len(string)):
  freq[i]=1
  for j in range(i+1,len(string)):
    if(string[i]==string[j]):
        freq[i]=freq[i]+1

        string=string[:j]+'0'+string[j+1:];

print("Character and their frequency");
for i in range(0,len(freq)):
    if(string[i]!=' ' and string[i]!='0'):
        print(string[i]+"="+str(freq[i]))

When the above code is executed, it produces the following result

Enter the string python
Character and their frequency
p=1
y=1
t=1
h=1
o=1
n=1

 

find the frequency of  the each character in the string -method 2

The program allows the user to enter a String and then it finds the frequency of the each character in the given string

Program 2

string=input("Enter the string: ")
str1=list(string)
strlist=[]

for j in str1:
    if j not in strlist:
       strlist.append(j)
       count=0

       for i in range(len(str1)):
         if j==str1[i]:
           count+=1
       print("{},{}".format(j,count))

When the above code is executed, it produces the following result

Enter the string: mypython
m,1
y,2
p,1
t,1
h,1
o,1
n,1

 

Suggested for you

for loop in Python language

while loop in Python language

if statements in Python

 

Similar post

C++ program to count the total number of characters in the given string

C program to count the total number of characters in the given string

Python program to count the total number of characters in the given string

 

Java program to count the frequebcy of a character of a string

C program to count the frequebcy of a character of a string

Python program to count the frequebcy of a character of a string

C++ program to count the frequebcy of a character of a string

C++ program: Find the frequency of each character in the string
Find Largest of three numbers using ternary operator in C++
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago