Problem Definition
Develop a Python program to count the number of times each character appears in the string that is the frequency of character in the string.
Video Tutorial
Steps using “in” operator
First, create a string variable, accept the string from the user and store it into a variable. In this case, str1 is a string variable.
Next, create an empty dictionary to store the character as key and frequency of character as value.
Loop over the string to get one character at a time (each iteration).
Check whether the character is already present in the dictionary using in operator.
If the character is not present then add the character into the dictionary with character as key and value set to 1.
If the character is already present in the dictionary then increase the value of the character by 1.
Finally, print the dictionary with characters as keys and values as the frequency of characters.
Python program to count the frequency of each character in the string using “in” operator
#Given a string and you want to count how many times each character appears str1 = input ("Enter the string: ") d = dict() for c in str1: if c in d: d[c] = d[c] + 1 else: d[c] = 1 print(d)
Output
Enter the string: VTU BGM VTU {'V': 2, 'T': 2, 'U': 2, ' ': 2, 'B': 1, 'G': 1, 'M': 1}
Use of “get()” function
get() function takes two parameters. It checks whether the first parameter is present in the dictionary as key if present then it returns the value of key otherwise zero.
For example, in the first case, ‘Jan’ is present hence its value 1 is returned. In the second case ‘may’ is not present hence 0 is returned.
months = { 'jan' : 1, 'feb' : 2, 'march': 3} print(months.get('jan', 0)) print(months.get('may', 0))
Output
1 0
Steps using “get()” function
First, create a string variable, accept the string from the user and store it into a variable. In this case, str1 is a string variable.
Next, create an empty dictionary to store the character as key and frequency of character as value.
Loop over the string to get one character at a time (each iteration).
Use get() the function to check the character is present in the dictionary, if present get function returns the value of character, then add one to it.
If the character is not present then the get() function returns 0, that is the second parameter, add 1 to it and put the character into the dictionary.
Finally, print the dictionary with characters as keys and values as the frequency of characters.
Python program to count the frequency of each character in the string using “get()” function
This tutorial#Given a string and you want to count how many times each character appears str1 = input ("Enter the string: ") d = dict() for c in str1: d[c] = d.get(c, 0) + 1 print(d)
Output
Enter the string: VTU BGM VTU {'V': 2, 'T': 2, 'U': 2, ' ': 2, 'B': 1, 'G': 1, 'M': 1}
Summary:
This tutorial discusses, how to develop a Python program to count the frequency of each character in the string. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.