In this article we will discuss different ways to count occurrences of a single character or some selected characters in a string and find their index positions in the string.


Count Occurrences of a single character in a String using string.count()

In Python the String class contains a method to count to the occurrences of a character or string in the string object i.e.

It looks for the character or string s in range start to end and returns it’s occurrence count. If start & end is not provided then it will look in complete string and return the occurrence count of s ( character or string) in the main string. Let’s use string.count() to count the occurrences of character ‘s’ in a big string i.e.

Output:

Count Occurrences of a single character in a String using collections.Counter()

Counter is a dict subclass and collections.Counter() accepts an iterable entity as argument and keeps the elements in it as keys and their frequency as value. So, if we pass a string in collections.Counter() then it will return a Counter class object which internally has characters as keys and their frequency in string as values. Let’s use that to find the occurrence count of character ‘s’ in a string i.e.

Output:

Counter() returned a Counter class ( sub-class of dict) object containing all characters in string as key and their occurrence count as value. We fetched the occurrence count of character ‘s’ from it using [] operator.

Python Regex : Count occurrences of a single character using regex

We can also find the frequency of a character in string using python regex i.e.

Output:

We created a regex pattern to match the character ‘s’ and the find all occurrences of character that matched our pattern i.e. all occurrences of character ‘s’ as list. It’s length gives us the occurrence count of character ‘s’ in string.

Using python regex for this is kind of over kill but it is really useful if we are looking to count the occurrences of multiple characters in a string.

Count occurrences of multiple characters in string using Python regex

We will create a regex pattern to match the either character ‘s’ or ‘c’ and the find all occurrences of characters that matched our pattern i.e. all occurrences of either character ‘s’ & ‘c’ as list. It’s length gives us the occurrence count of both the characters in the string. For example,

Output:

Finding index positions of single or multiple characters in a string

Count Occurrences and find all index position of a single character in a String

To find the index positions of a given character in string using regex, create a regex pattern to match the character. Then iterate over all the matches of that pattern in the string and add their index positions to a list i.e.

Output

Find Occurrence count and index position of a multiple character in a String

Similarly we can find the index positions of multiple characters in the string i.e.

Output:

Complete example is as follows,

Output:

 

 

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,