How to Count Occurrences of a Character in a String in Python?

The strings in Python are nothing but a sequence of characters/numbers. In python, multiple functions are used to perform various operations on the strings. These operations include replacing, updating, appending, counting characters occurrences, etc. Various functions are used in Python to count the occurrences of a character in a string.

This post will elaborate on all the methods to count occurrences of a character in a string.

Method 1: Using count() Function

The “count()” function retrieves the number of times something appears in an iterator. In the below code, the “count()” function is used to count the occurrences of a character “P”:

Code:

string_value = "Python Guide Provided By itslinuxfoss"
print(string_value.count('P'))

In the above code, the “count()” function accepts the “P” character as an argument and counts the number of occurrences of this character in a string.

Output:

The above output shows a string’s total number of character “P” occurrences.

Note: The count() function is case-sensitive; this means it will consider “P” and “p” two different characters.

Method 2: Using for loop to Count Occurrences of Character

The “for” loop is also used to determine how many times a character appears in a string. Here is an example code:

Code:

string_value = "Python Guide Provided By itslinuxfoss"
counter_value = 0
for item in string_value:
    if item == "P":
        counter_value = counter_value + 1
print(counter_value)

In the above code, the string, and the counter value is initialized in the program. To check and count the occurrence of the character “P” in the string, the for loop is used along with the “if” condition.

Output:

The above output shows that the character “P” occurs two times in the given string.

Method 3: Using re.findall() Method

The “re.findall()” method is used to find all the matches of a character and returns them as a list. In the below code, the “len()” function is used along with “re.findall()” to find the count occurrences of a character in a string.

Code:

import re
string_value = "Python Guide Provided By itslinuxfoss"
output = re.findall("o", string_value)
print(len(output))

In the above code, the “re.findall()” function finds all the matches of the character “o” in a given string. The “len()” function accepts the list of strings and returns the length of occurrences.

Output:

The above output shows the total occurrences of the character “o” in a given string.

Method 4: Using lambda Expression

The “lambda” function is an anonymous function used to evaluate the arguments and return the result as one single expression. In the following code block, the “map()” function is utilized with the lambda function to count the character occurrences:

Code:

string_value = "Python Guide Provided By itslinuxfoss"
output = map(lambda item : 1 if 'i' in item else 0, string_value)
print(sum(output))

In the above code, the “map()” function accepts the lambda function as a first argument and string value as a second argument. The map() function will retrieve a map object(iterator). Finally, the sum() function counts the specified character occurrences in a string.

Output:

The above output shows the total occurrences of the character “i” in a given string.

Method 5: Using collection.Counter()

The “counter()” function of the “collection” module is used to calculate the number of a character that appears in a string. Here is an example:

Code:

import collections
string_value = "Python Guide Provided By itslinuxfoss"
counter = collections.Counter(string_value)
output = counter['i']
print(output)

In the above code, the “collection.counter()” function accepts the string as an argument and stores the value in the variable “counter”. The “counter[‘i’]” will return the total character “i” occurrences in a string.

Output:

The above output shows the total occurrences of the character “i” in a given string.

Method 6: Using operator.countOf() Method

The “countOf()” method is also used to count the total character that occurs in a string. Let’s understand the below code:

Code:

import operator
string_value = "Python Guide Provided By itslinuxfoss"
output = operator.countOf(string_value, "i")
print(output)

In the above code, the “operator.countOf()” is used to count the number of times the character “i” is found in a given string.

Output:

This output displays the entire number of occurrences of “i” in the string.

Conclusion

To count the occurrences of a character in a string, the count() function, for loop, re.findall() function, lambda expression, etc., are used in Python. The “count()” function accepts the character value as an argument and finds all its occurrences in the string. Similarly, the “re.findall()” method finds all the matches of characters in a string and returns the list of strings that can be passed to the len() function to get the total occurrences of the targeted character. Various methods are presented in this post to count the number of times a character occurs in a Python string.