All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Count Occurrences Of Character In String Python

Last Updated : Mar 11, 2024

Count Occurrences Of Character In String Python

In this article we will show you the solution of count occurrences of character in string python, the aim is to determine how often a particular character appears in a given string.

Many applications, including deleting duplicates and identifying undesirable characters, benefit from this particular function on strings.

Using Python's built-in string.count() method is one of the predefined ways to count a number of instances in a string.

The function takes the number of times particular character appears in the string linked to the method for one parameter, which can be either a character or even a substring.

Go on to the idea of counting the number of times a character appears in a string in Python.

Step By Step Guide On Count Occurrences Of Character In String Python :-

Method - Naive

test_str = "Talkerscode"
 count = 0
for i in test_str:
    if i == 'e':
        count = count + 1
print ("Count of e in Talkerscode is : " + str(count))
  1. You can see that we created a Python programme in this case to show how the naive method can be used to determine occurrence frequency.
  2. Users are able to observe that we initialise the string referred to as Talkerscode at the beginning of the code.
  3. To obtain the character e's count from the existing string, we then employ a naive technique.
  4. You can iterate through the full string looking for that specific character through the for loop naive technique.
  5. after which, when we come across the specific character, raise the counter.
  6. Finally, using Python's print function, we output the results, which represent the character count.

Method - collections.Counter()

from collections import Counter
test_str = "Talkerscode"
count = Counter(test_str)
print ("Count of e in Talkerscode is : "+ str(count['e']))
  1. You can see that we wrote the Python code in this example to use collections to show how to calculate occurrence collections.Counter().
  2. We import the counter package from the collections at the beginning of the code.
  3. Key-value pairs can be counted in an object, often known as a hashtable object, to use the Python Counter tool.
  4. The values for the count and the keys are represented by the items in this sentence. An iterable list's items can be counted using this method.
  5. Next, we use the collections after initialising the Talkerscode string.
  6. To determine the character count, use Counter().
  7. The use of collections.counter(), the less well-known Python method for determining how often an element appears in any container.
  8. This also does the same purpose as the first two methods, but it works with a different library, namely collections.
  9. As a last step, we use Python's print function to show the output in result form, which is the count of characters that are now present in the string.

Conclusion :-

So, we were able to understand the idea of counting the occurrences of a character in a string of Python.

We also discovered that the goal is to ascertain how frequently a specific character appears in such a given string.

This particular function on strings has many uses, such as removing duplicates and identifying undesired characters.

I hope this article on count occurrences of character in string python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪