Number of Occurrences of a Character in a String Python
When analyzing text data, it may be necessary to count the frequency of a character in the text. In numerous instances, such as eliminating duplicates or detecting undesirable characters, this particular operation on a string is beneficial.
In this section, we'll look at various Python methods for counting the instances of each character in a string.
Different Ways to Count Occurrences of a Character in a String
There are several ways to determine how many times a character appears in a string. Following is a list of the several approaches we can take to determine how many times a certain character appears in a string:
- Native Method
- Count() Method
- Operator.countOf() Method
- Collections.Counter() Method
- Regular Expressions
- Lambda Functions
- Reduce() Method
Counting the Character's Occurrence in a String using the Native Method
In the Native approach, we iterate the entire string looking for that specific character, and once we find it, we raise the counter.
Python Program for counting the frequency of a character in a String using a Native approach
string=input("Enter the string: ")
char1=input("Enter the character: ")
c = 0
for i in string:
if i == char1:
c = c + 1
print("Count of",char1,"in",string,"is: "+ str(c))
Output
![Number of Occurrences of a Character in a String Python](https://static.tutorialandexample.com/python/number-of-occurrences-of-a-character-in-a-string-python1.png)
The above code's Time Complexity is O(n), where n is the string length.
The above code's auxiliary space is O(1), where an additional count variable stores the result.
Count the Character's Occurrence in a String usingthe count() Method.
The most common way to determine how often an element appears in any collection in Python is to use the count() function. It is also popular since it is simple to remember and code.
Python program for counting the frequency of a character in the given text employing the count() method
string = input("Enter the string: ")
char=input("Enter the character: ")
count_char = string.count(char)
print("Count of", char,"in the string", string," is : "+ str(count_char))
Output
![Number of Occurrences of a Character in a String Python](https://static.tutorialandexample.com/python/number-of-occurrences-of-a-character-in-a-string-python2.png)
The above code's time complexity is O(n)
The above code
's auxiliary space is O(n)
Count the Number of Occurrences in a Python String using the operator.countOf() Method
To count the frequency of b in a the operator.Countof() method is employed. It keeps track of how frequently value occurs.
Python program for counting the frequency of a character in the given text employing the operator.countOf() Method
import operator as op
string = input("Enter the string: ")
char=input("Enter the character: ")
count = op.countOf(string, char)
print("Count of",char,"in the string",string," is : "+ str(count))
Output
![Number of Occurrences of a Character in a String Python](https://static.tutorialandexample.com/python/number-of-occurrences-of-a-character-in-a-string-python3.png)
The above code's time complexity is O(n)
The auxiliary space for the above code is O(1)
Count the Character's Occurrence in a String using the Counter method.
Using collections.Counter() function, one can determine how often an element appears in a given collection. The goal of this is similar to that of the initial two strategies, but it makes use of a distinct library called collections. This method is less common..
Python program for counting the frequency of a character in the given text employing the counter() Method
from collections import Counter
string = input("Enter the string: ")
char=input("Enter the character: ")
count = Counter(string)
print("Count of",char,"in the string",string," is : "+ str(count[char]))
Output
![Number of Occurrences of a Character in a String Python](https://static.tutorialandexample.com/python/number-of-occurrences-of-a-character-in-a-string-python4.png)
The above code's time complexity is O(n) (Linear time complexity)
Auxiliary space for the above code is O(k) (here k is the number of distinct characters)
Count the Character's Occurrence in a String using Regular Expressions
Many coding tasks involving strings can be accomplished using the regular expressions re + findall(). They can also help us with the challenge of locating the occurrence of an element in a string.
Python program for counting the number of occurrences of a character in the given text employing Regular Expressions
import re
string = input("Enter the string: ")
char=input("Enter the character: ")
count = len(re.findall(char, string))
print("Count of",char,"in the string",string," is : "+ str(count))
Output
![Number of Occurrences of a Character in a String Python](https://static.tutorialandexample.com/python/number-of-occurrences-of-a-character-in-a-string-python5.png)
The above code's time complexity is O(n)
The auxiliary space for the above code is O(n)
Count the Character's Occurrence in a String using the Lambda Function
lambda + sum() + map() is the lambda function. It is possible to do the specific task of counting the total instances of a specific element in a string using the sum() and map() functions. This uses sum() to add all the map()-retrieved occurrences.
Python program for counting the number of occurrences of a character in the given text employing lambda
string = input("Enter the string: ")
char=input("Enter the character: ")
count = sum(map(lambda c: 1 if char in c else 0, string))
print("Count of",char,"in the string",string," is : "+ str(count))
Output
![Number of Occurrences of a Character in a String Python](https://static.tutorialandexample.com/python/number-of-occurrences-of-a-character-in-a-string-python6.png)
The above code's time complexity is O(n) (Linear time complexity)
Auxiliary Space for the above code isO(1) (Constant Space complexity)
Count the Character's Occurrence in a String using Reduce() Method.
We can find the frequency of each character in a string utilizing the reduce() method. There are lambda functions accessible. A character c and an accumulator acc are the two arguments that a lambda function must accept. The lambda function determines whether character c and character_to_count are equal. Apply the lambda function and the initial value 0 to the input string test_string using the reduce() Method. Begining with the initial value 0, the lambda function is applied by the reduce() function to each character in the string test_string. The count of the character char_count in the string test_string is the final value of the accumulator accum. The count variable keeps track of the outcome. Employ an f-string to format the result before printing it.
Python program for counting the number of occurrences of a character in the given text employing Reduce() Method
from functools import reduce
test_string = input("Enter the string: ")
char_count = input("Enter the character: ")
count = reduce(lambda accum, c: accum + 1 if c ==char_count else accum, test_string, 0)
print(f"Count of {char_count} in {test_string} is: {count}")
Output
![Number of Occurrences of a Character in a String Python](https://static.tutorialandexample.com/python/number-of-occurrences-of-a-character-in-a-string-python7.png)
The above code's time complexity is O(n), here n is the length of the input string test_string. This is due to the fact that we loop over each character in the string exactly one time.
We just require a small amount of extra space since the input string, the character to count, the accumulator accum, and the lambda function are all saved; hence, the auxiliary space of this code is O(1). The length of the input string has no bearing on how much space is required as a result.