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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago