Count Occurrences of a Character in String in Python Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a string, and our task is to count how many times a specific character appears in it using Python. This can be done using methods like .count(), loops, or collections.Counter. For example, in the string "banana", using "banana".count('a') will return 3 since the letter 'a' appears three times.Using count()The built-in count() method in the string class makes it easy to count how many times a character appears in a string. This is ideal for counting a single character quickly and easily. Python s = "apple" cnt = s.count('p') print(cnt) Output2 Explanation: The string "apple" contains two occurrences of character 'p'.Using a LoopIf we want more control over the counting process then we can use a loop (for loop) to iterate through each character in the string. Python s = "hello world" t = 'l' cnt = 0 for c in s: if c == t: cnt += 1 print(cnt) Output3 Explanation: This code counts how many times the character 'l' appears in the string "hello world" using a loop. It iterates through each character in the string s, and if the character matches t, it increments the counter cnt. Finally, it prints the total count.Using collections.CounterThe Counter class from collections module is a simple and efficient way to count how many times each character appears in a string. It automatically creates a count for each character and making it easy to see how many of each character are present in the string. This is best option for efficiently counting all characters in a string with clear and concise code. Python from collections import Counter s = "GeeksforGeeks" cnt = Counter(s) print(cnt['e']) Output4 Explanation:Counter(s) counts how many times each character appears in the string.To find out how many times the character 'e' appears, just use count['e'], which gives us 4.Related Articles:Python String count() MethodPython For LoopsPython collections Counter Comment More infoAdvertise with us Next Article Count occurrences of an element in a list in Python M manjeet_04 Follow Improve Article Tags : Python python-string Python string-programs Practice Tags : python Similar Reads Count the number of Unique Characters in a String in Python We are given a string, and our task is to find the number of unique characters in it. For example, if the string is "hello world", the unique characters are {h, e, l, o, w, r, d}, so the output should be 8.Using setSet in Python is an unordered collection of unique elements automatically removing du 2 min read Python | Count the Number of matching characters in a pair of string The problem is about finding how many characters are the same in two strings. We compare the strings and count the common characters between them. In this article, we'll look at different ways to solve this problem.Using Set Sets are collections of unique items, so by converting both strings into se 2 min read Python | Count the Number of matching characters in a pair of string The problem is about finding how many characters are the same in two strings. We compare the strings and count the common characters between them. In this article, we'll look at different ways to solve this problem.Using Set Sets are collections of unique items, so by converting both strings into se 2 min read Count occurrences of an element in a list in Python A common task when working with lists is to count how many times a specific element appears. In Python, we have several ways to count occurrences of an element using both built-in and custom methods.The simplest and most straightforward way to count occurrences of an element in a list is by using th 2 min read Find all duplicate characters in string in Python In this article, we will explore various methods to find all duplicate characters in string. The simplest approach is by using a loop with dictionary.Using Loop with DictionaryWe can use a for loop to find duplicate characters efficiently. First we count the occurrences of each character by iteratin 2 min read Python - Replacing Nth occurrence of multiple characters in a String with the given character Replacing the Nth occurrence of multiple characters in a string with a given character involves identifying and counting specific character occurrences.Using a Loop and find()Using a loop and find() method allows us to search for the first occurrence of a substring within each list element. This app 2 min read Like