{"id":5649,"date":"2023-10-28T10:41:33","date_gmt":"2023-10-28T05:11:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=5649"},"modified":"2023-11-10T12:05:10","modified_gmt":"2023-11-10T06:35:10","slug":"count-occurrences-of-a-single-or-multiple-characters-in-string-and-find-their-index-positions","status":"publish","type":"post","link":"https:\/\/python-programs.com\/count-occurrences-of-a-single-or-multiple-characters-in-string-and-find-their-index-positions\/","title":{"rendered":"Count Occurrences of a Single or Multiple Characters in String and Find their Index Positions"},"content":{"rendered":"
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\u2019s and 1\u2019s internally.<\/p>\n
In this article we are going to count single characters \/multiple characters in a given string.<\/p>\n
Examples:<\/strong><\/p>\n 1)Count occurrences of single characters in a given string<\/strong><\/p>\n Input:\u00a0<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Count occurrences of multiple characters in a\u00a0 given string<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several methods\u00a0 some of them are:<\/p>\n 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.<\/p>\n 1)Count occurrence of single character using count() function<\/strong><\/p>\n We can count the occurrence of single character using count() as given below.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Count occurrence of multiple characters using count() function<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n 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.<\/p>\n 1)Count occurrence of single character using Counter() function<\/strong><\/p>\n Counting the frequency of character using Counter() function<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Count occurrence of multiple characters using Counter() function<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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\u2019s and 1\u2019s internally. In this article we are going to count single characters \/multiple characters in a given string. Examples: 1)Count occurrences …<\/p>\ngiven_string=\"btechgeeks\" character ='e'<\/pre>\n
count of e is : 3<\/pre>\n
given_string=\"btechgeeks\" character =['e' , 'c' , 's' ]<\/pre>\n
count of e is : 3\r\ncount of c is : 1\r\ncount of s is : 1<\/pre>\n
Count occurrences of single\/multiple characters in a given string and indices of them<\/strong><\/h2>\n
\n
Method #1:Using count() function<\/h3>\n
# given string\r\nstring = \"btechgeeks\"\r\n# given character which should be counted\r\ncharacter = 'e'\r\n# counting the number of occurences of given character in the string\r\ncharcount = string.count(character)\r\nprint(\"count of \", character, \"is :\", charcount)\r\n<\/pre>\n
count of e is : 3<\/pre>\n
\n
# given string\r\nstring = \"btechgeeks\"\r\n# given characters list which should be counted\r\ncharlist = ['e', 'c', 's']\r\n# traverse the charlist\r\nfor char in charlist:\r\n # counting the number of occurences of given character in the string\r\n charcount = string.count(char)\r\n print(\"count of \", char, \"is :\", charcount)\r\n<\/pre>\n
count of e is : 3\r\ncount of c is : 1\r\ncount of s is : 1<\/pre>\n
Method #2:Using Counter() function which is in collections module<\/h3>\n
from collections import Counter\r\n# given string\r\nstring = \"btechgeeks\"\r\n# given character which should be counted\r\ncharacter = 'e'\r\n# counting the frequency of all characters using counter() function\r\nfreq = Counter(string)\r\n# counting the number of occurences of given character in the string using [] operator\r\ncharcount = freq[character]\r\nprint(\"count of \", character, \"is :\", charcount)\r\n<\/pre>\n
count of e is : 3<\/pre>\n
\n
from collections import Counter\r\n# given string\r\nstring = \"btechgeeks\"\r\n# given characters list which should be counted\r\ncharlist = ['e', 'c', 's']\r\n# counting the frequency of all characters using counter() function\r\nfreq = Counter(string)\r\n# traverse the charlist\r\nfor char in charlist:\r\n # counting the number of occurences of given character in the string using [] operator\r\n charcount = freq[char]\r\n print(\"count of \", char, \"is :\", charcount)\r\n<\/pre>\n
count of e is : 3\r\ncount of c is : 1\r\ncount of s is : 1<\/pre>\n
Finding single character index positions in a string<\/h2>\n
Method #3:Using regex to count and print indices of occurrences of a string<\/h3>\n
import re\r\n# given main string\r\nstring = 'Hello this is BTechGeeks'\r\n# given character\r\nchar = 'e'\r\n# Make a regex pattern that matches the given character\r\npattern = re.compile(char)\r\n# Iterate over all the matches of regex pattern\r\nmatchiterator = pattern.finditer(string)\r\n# taking empty list\r\nindexlist = []\r\n# initializing count to 0\r\ncharcount = 0\r\nfor matchobject in matchiterator:\r\n indexlist.append(matchobject.start())\r\n charcount = charcount + 1\r\nprint(\"Count of given character \", char, \"is :\", charcount)\r\nprint(\"indices of given characters :\", indexlist)\r\n<\/pre>\n
Count of given character e is : 4\r\nindices of given characters : [1, 16, 20, 21]<\/pre>\n
\n