Python count char in string

Count characters and strings in Python

This article explains how to count the number of specific characters (letters) or substrings within a string ( str ) in Python.

For details on how to read a text file as a string, calculate the length (the total character count) of a string, or search for a substring within a string, please refer to the following articles:

Count characters and substrings: count()

The count() method allows you to count the number of specific characters or substrings within a string.

s = 'abc_aabbcc_abc' print(s.count('abc')) # 2 print(s.count('a')) # 4 print(s.count('xyz')) # 0 

If the second argument start and the third argument end are specified, the range of the slice [start:end] is targeted.

print(s.count('a', 4, 10)) # 2 print(s[4:10]) # aabbcc print(s[4:10].count('a')) # 2 

Like slicing, a negative value can specify a position from the end. If end is omitted, the range is up to the end.

print(s.count('a', -9)) # 2 print(s[-9:]) # abbcc_abc print(s[-9:].count('a')) # 2 

count() only counts non-overlapping occurrences of the specified substring. Each character is counted only once.

s = 'abc_abc_abc' print(s.count('abc_abc')) # 1 

To count overlapping substrings, use the regular expression described below.

Count the number of words

For example, if you want to count «am» with the count() method, «Sam» is also counted.

s = 'I am Sam' print(s.count('am')) # 2 

To tally specific words, you can use the split() method, dividing the string into a list of words using a specified delimiter, such as spaces or punctuation. You can then use the count() method on the list to count exact word matches.

l = s.split() print(l) # ['I', 'am', 'Sam'] print(l.count('am')) # 1 

For long sentences, the Counter class of the standard Python library collections is useful for counting the frequency of each word. See the following article.

Читайте также:  Жирный шрифт в javascript

Keep in mind that using split() to divide a string into words is a basic approach. Since actual sentences may contain various symbols, it is safe to use a natural language processing library such as NLTK.

Count with regex: re.findall()

Use re.findall() to count substrings that match a regex pattern.

re.findall() returns a list of all substrings that match the pattern. Use the built-in len() function to get the total count of matched substrings.

import re s = '123-456-789' print(re.findall('2 ', s)) # ['123', '456', '789'] print(len(re.findall('8 ', s))) # 3 

In the example above, 5 is a regex pattern matching any three-digit number.

You can also count overlapping substrings using a lookahead assertion (? =. ) and grouping () .

s = 'abc_abc_abc' print(re.findall('(?=(abc_abc))', s)) # ['abc_abc', 'abc_abc'] print(len(re.findall('(?=(abc_abc))', s))) # 2 s = '12345' print(re.findall('(?=(3 ))', s)) # ['123', '234', '345'] print(len(re.findall('(?=(5 ))', s))) # 3 

For more information on the re module, see the following article.

Case-insensitive counting

s = 'abc_ABC' print(s.count('abc')) # 1 

For case-insensitive counting, you can convert the string to upper or lower case. Use upper() to make a string all uppercase and lower() to make it all lowercase.

print(s.lower()) # abc_abc print(s.lower().count('abc')) # 2 print(s.upper()) # ABC_ABC print(s.upper().count('ABC')) # 2 

With regex, you can set re.IGNORECASE as the flags parameter in functions like re.findall() for case-insensitive counting.

print(re.findall('abc', s, flags=re.IGNORECASE)) # ['abc', 'ABC'] print(re.findall('ABC', s, flags=re.IGNORECASE)) # ['abc', 'ABC'] 

Источник

Count Occurrences of a Character in a String in Python

Count Occurrences of a Character in a String in Python

  1. Use the count() Function to Count the Number of a Characters Occuring in a String in Python
  2. Use the collections.Counter to Count the Occurrences of a Character in a String in Python
  3. Use Regular Expressions to Count the Occurrences of a Character in a String in Python
  4. Use the defaultdict to Count the Occurrences of a Character in a String in Python
  5. Use the pandas.value_counts() to Count the Occurrences of a Character in a String in Python
  6. Use a lambda Expression to Count the Occurrences of a Character in a String in Python
  7. Use the for Loop to Count the Occurrences of a Character in a String in Python

In Programming, a string is a sequence of characters.

This tutorial will introduce how to count the number of occurrences of a character in a String in Python.

Use the count() Function to Count the Number of a Characters Occuring in a String in Python

We can count the occurrence of a value in strings using the count() function. It will return how many times the value appears in the given string.

print('Mary had a little lamb'.count('a')) 

Remember, upper and lower cases are treated as different characters. A and a will be treated as different characters and have different counts.

Use the collections.Counter to Count the Occurrences of a Character in a String in Python

A Counter is a dictionary subclass present in the collections module. It stores the elements as dictionary keys, and their occurrences are stored as dictionary values. Instead of raising an error, it returns a zero count for missing items.

from collections import Counter my_str = "Mary had a little lamb" counter = Counter(my_str) print(counter['a']) 

It is a better choice when counting for many letters as counter calculates all the counts one time. It is a lot faster than the count() function.

Use Regular Expressions to Count the Occurrences of a Character in a String in Python

A regular expression is a specialized syntax held in a pattern that helps find the strings or set of strings by matching that pattern. We import the re module to work with regular expressions.

We can use the findall() function for our problem.

import re my_string = "Mary had a little lamb" print(len(re.findall("a", my_string))) 

Use the defaultdict to Count the Occurrences of a Character in a String in Python

Defaultdict is present in the collections module and is derived from the dictionary class. Its functionality is relatively the same as that of dictionaries except that it never raises a KeyError , as it provides a default value for the key that never exists.

We can use it to get the occurrences of a character in a string as shown below.

from collections import defaultdict  text = 'Mary had a little lamb' chars = defaultdict(int)  for char in text:  chars[char] += 1  print(chars['a']) print(chars['t']) print(chars['w']) # element not present in the string, hence print 0 

Use the pandas.value_counts() to Count the Occurrences of a Character in a String in Python

We can use the pandas.value_counts() method to get the occurrences of all the characters present in the provided string. We need to pass the string as a Series object.

import pandas as pd phrase = "Mary had a little lamb" print(pd.Series(list(phrase)).value_counts()) 
 4 a 4 l 3 t 2 e 1 b 1 h 1 r 1 y 1 M 1 m 1 i 1 d 1 dtype: int64 

It returns the occurrences of all characters in a Series object.

Use a lambda Expression to Count the Occurrences of a Character in a String in Python

lambda functions can not only count occurrences from the given string, but can also work when we have the string, as a list of sub-strings.

sentence = ['M', 'ar', 'y', 'had', 'a', 'little', 'l', 'am', 'b'] print(sum(map(lambda x : 1 if 'a' in x else 0, sentence))) 

Use the for Loop to Count the Occurrences of a Character in a String in Python

We iterate over the string, and if the element equals the desired character, the count variable is incremented till we reach the end of the string.

sentence = 'Mary had a little lamb' count = 0 for i in sentence:  if i == "a":  count = count + 1 print(count) 

We can see another way of using this method with the sum() function can be seen below.

my_string = "Mary had a little lamb" print(sum(char == 'a' for char in my_string)) 

Related Article — Python String

Источник

Оцените статью