Calculate frequency of each character in string

In this program, we will take a string as input and calculate the frequency of each character in a string.

Steps to solve the program
  1. Take a string as input and create an empty dictionary.
  2. Use For loop to iterate over each character in the string.
  3. Add the character as a key in the dictionary and its count in the string as its value.
  4. Use count() to count how many times a character has appeared in the string.
  5. Print the output.
				
					#Input string
string = "sqatools"
dictionary = dict()

for char in string:
    dictionary[char] = string.count(char)

#Printing output    
print(dictionary)
				
			

Output :

				
					{'s': 2, 'q': 1, 'a': 1, 't': 1, 'o': 2, 'l': 1}
				
			

calculate the length of a string.

combine two strings into one.

Leave a Comment