Count Occurrences of a Single or Multiple Characters in String and Find their Index Positions

A Python string is a collection of characters surrounded by single, double, or triple quotes. The computer does not understand the characters; instead, it stores the manipulated character as a combination of 0’s and 1’s internally.

In this article we are going to count single characters /multiple characters in a given string.

Examples:

1)Count occurrences of single characters in a given string

Input: 

given_string="btechgeeks" character ='e'

Output:

count of  e is : 3

2)Count occurrences of multiple characters in a  given string

Input:

given_string="btechgeeks" character =['e' , 'c' , 's' ]

Output:

count of  e is : 3
count of  c is : 1
count of  s is : 1

Count occurrences of single/multiple characters in a given string and indices of them

There are several methods  some of them are:

Method #1:Using count() function

The count() method in the string class returns the number of times a substring appears in the string. In simple terms, the count() method looks for a substring in a string and returns the number of times it appears.

1)Count occurrence of single character using count() function

We can count the occurrence of single character using count() as given below.

Below is the implementation:

# given string
string = "btechgeeks"
# given character which should be counted
character = 'e'
# counting the number of occurences of given character in the string
charcount = string.count(character)
print("count of ", character, "is :", charcount)

Output:

count of  e is : 3

2)Count occurrence of multiple characters using count() function

  • Traverse the list of characters whose frequency is to be calculated.
  • Using count function print the frequency of respective characters.

Below is the implementation:

# given string
string = "btechgeeks"
# given characters list which should be counted
charlist = ['e', 'c', 's']
# traverse the charlist
for char in charlist:
    # counting the number of occurences of given character in the string
    charcount = string.count(char)
    print("count of ", char, "is :", charcount)

Output:

count of  e is : 3
count of  c is : 1
count of  s is : 1

Method #2:Using Counter() function which is in collections module

Counter is a set and dict subset. Counter() takes an iterable entity as an argument and stores the elements as keys and the frequency of the elements as a value. So, in collections, if we transfer a string. When you call Counter(), you’ll get a Counter class object with characters as keys and their frequency in a string as values.

Counter() returns a Counter type object (a subclass of dict) with all characters in the string as keys and their occurrence count as values. We’ll use the [] operator to get the occurrence count of the character’s’ from it.

1)Count occurrence of single character using Counter() function

Counting the frequency of character using Counter() function

Below is the implementation:

from collections import Counter
# given string
string = "btechgeeks"
# given character which should be counted
character = 'e'
# counting the frequency of all characters using counter() function
freq = Counter(string)
# counting the number of occurences of given character in the string using [] operator
charcount = freq[character]
print("count of ", character, "is :", charcount)

Output:

count of  e is : 3

2)Count occurrence of multiple characters using Counter() function

  • Traverse the list of characters whose frequency is to be calculated.
  • We can count their frequency using [] operator

Below is the implementation:

from collections import Counter
# given string
string = "btechgeeks"
# given characters list which should be counted
charlist = ['e', 'c', 's']
# counting the frequency of all characters using counter() function
freq = Counter(string)
# traverse the charlist
for char in charlist:
    # counting the number of occurences of given character in the string using [] operator
    charcount = freq[char]
    print("count of ", char, "is :", charcount)

Output:

count of  e is : 3
count of  c is : 1
count of  s is : 1

Finding single character index positions in a string

Method #3:Using regex to count and print indices of occurrences of a string

Build a regex pattern that matches the character to find the index positions of that character in a string. Then iterate through all of the pattern matches in the string, adding their index positions to a list.

Below is the implementation:

import re
# given main string
string = 'Hello this is BTechGeeks'
# given character
char = 'e'
# Make a regex pattern that matches the given character
pattern = re.compile(char)
# Iterate over all the matches of regex pattern
matchiterator = pattern.finditer(string)
# taking empty list
indexlist = []
# initializing count to 0
charcount = 0
for matchobject in matchiterator:
    indexlist.append(matchobject.start())
    charcount = charcount + 1
print("Count of given character ", char, "is :", charcount)
print("indices of given characters :", indexlist)

Output:

Count of given character  e is : 4
indices of given characters : [1, 16, 20, 21]

Related Programs: