# Python | Count occurrences of a character in string

• Difficulty Level : Easy
• Last Updated : 22 Nov, 2018

Given a string, the task is to count the frequency of a single character in that string. This particular operation on string is quite useful in many applications such as removing duplicates or detecting unwanted characters.

Method #1 : Naive method

Iterate the entire string for that particular character and then increase the counter when we encounter the particular character.

 `# Python3 code to demonstrate ``# occurrence frequency using ``# naive method `` ` `# initializing string ``test_str ``=` `"GeeksforGeeks"`` ` `# using naive method to get count ``# counting e ``count ``=` `0`` ` `for` `i ``in` `test_str:``    ``if` `i ``=``=` `'e'``:``        ``count ``=` `count ``+` `1`` ` `# printing result ``print` `(``"Count of e in GeeksforGeeks is : "``                            ``+`  `str``(count))`

Output :

`Count of e in GeeksforGeeks is : 4`

Method #2 : Using `count()`

Using `count()` is the most conventional method in Python to get the occurrence of any element in any container. This is easy to code and remember and hence quite popular.

 `# Python3 code to demonstrate ``# occurrence frequency using ``# count()`` ` `# initializing string ``test_str ``=` `"GeeksforGeeks"`` ` `# using count() to get count ``# counting e ``counter ``=` `test_str.count(``'e'``)`` ` `# printing result ``print` `(``"Count of e in GeeksforGeeks is : "``                           ``+`  `str``(counter))`

Output :

```Count of e in GeeksforGeeks is : 4
```

Method #3 : Using `collections.Counter()`

This is the lesser known method to get the occurrence of the element across any container in Python. This also performs the task similar to above two method, just is a function of a different library i.e collections.

 `# Python3 code to demonstrate ``# occurrence frequency using ``# collections.Counter()``from` `collections ``import` `Counter`` ` `# initializing string ``test_str ``=` `"GeeksforGeeks"`` ` `# using collections.Counter() to get count ``# counting e ``count ``=` `Counter(test_str)`` ` `# printing result ``print` `(``"Count of e in GeeksforGeeks is : "``                       ``+`  `str``(count[``'e'``]))`

Output :

```Count of e in GeeksforGeeks is : 4
```

Method #4 : Using lambda + `sum()` + `map()`

Lambda functions, along with `sum()` and` map()` can achieve this particular task of counting the total occurrences of particular element in a string. This uses` sum()` to sum up all the occurrences obtained using` map()`.

 `# Python3 code to demonstrate ``# occurrence frequency using ``# lambda + sum() + map()`` ` `# initializing string ``test_str ``=` `"GeeksforGeeks"`` ` `# using lambda + sum() + map() to get count ``# counting e ``count ``=` `sum``(``map``(``lambda` `x : ``1` `if` `'e'` `in` `x ``else` `0``, test_str))`` ` `# printing result ``print` `(``"Count of e in GeeksforGeeks is : "``                            ``+`  `str``(count))`

Output :

```Count of e in GeeksforGeeks is : 4
```

Method #5 : Using `re + findall()`

Regular Expressions can help us to achieve many coding tasks related to strings. They can also facilitate us in achieving the task of finding the occurrence of element in string.

 `# Python3 code to demonstrate ``# occurrence frequency using ``# re + findall()``import` `re`` ` `# initializing string ``test_str ``=` `"GeeksforGeeks"`` ` `# using re + findall() to get count ``# counting e ``count ``=` `len``(re.findall(``"e"``, test_str))`` ` `# printing result ``print` `(``"Count of e in GeeksforGeeks is : "``                            ``+`  `str``(count))`

Output :

```Count of e in GeeksforGeeks is : 4
```

My Personal Notes arrow_drop_up