
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Frequency of each character in String in Python
Text processing has emerged as an important field in machine learning and AI. Python supports this filed with many available tools and libraries. In this article we will see how we can find the number of occurrence of each letter of a given string.
With Counter
The Counter method counts the number of occurrences of an element in an iterable. So it is straight forward to use by passing the required string into it.
Example
from collections import Counter # Given string strA = "timeofeffort" print("Given String: ",strA) # Using counter res = {} for keys in strA: res[keys] = res.get(keys, 0) + 1 # Result print("Frequency of each character :\n ",res)
Output
Running the above code gives us the following result −
Output
Given String: timeofeffort Frequency of each character : {'t': 2, 'i': 1, 'm': 1, 'e': 2, 'o': 2, 'f': 3, 'r': 1}
With get()
We can treat the string as a dictionary and count the keys for each character using get() in a for loop.
Example
# Given string strA = "timeofeffort" print("Given String: ",strA) # Using counter res = {} for keys in strA: res[keys] = res.get(keys, 0) + 1 # Result print("Frequency of each character :\n ",res)
Output
Running the above code gives us the following result −
Given String: timeofeffort Frequency of each character : {'t': 2, 'i': 1, 'm': 1, 'e': 2, 'o': 2, 'f': 3, 'r': 1}
With set
A set in python stores unique elements. So we can use it wisely by counting the number of times the same character is encountered again and again when looping through the string as an iterable.
Example
# Given string strA = "timeofeffort" print("Given String: ",strA) # Using counter res = {} res={n: strA.count(n) for n in set(strA)} # Result print("Frequency of each character :\n ",res)
Output
Running the above code gives us the following result −
Given String: timeofeffort Frequency of each character : {'f': 3, 'r': 1, 'm': 1, 'o': 2, 'i': 1, 't': 2, 'e': 2}
- Related Articles
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string in Python
- Find frequency of each word in a string in Python
- Python – Sort String list by K character frequency
- Encoding string based on character frequency in JavaScript
- Java Program to Find the Frequency of Character in a String
- Golang program to find the frequency of character in a string
- Python program to find occurrence to each character in given string
- Python – Insert character in each duplicate string after every K elements
- Find frequency of each word in a string in Java
- Find frequency of each word in a string in C#
- Print number of words, vowels and frequency of each character
- C++ Program to Find the Frequency of a Character in a String
- Check if the frequency of any character is more than half the length of the string in Python
- Java program to find the Frequency of a character in a given String
- Java program to check occurence of each character in String
