count characters in string python

23

>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
4
# Basic syntax:
import re
len(re.findall('[characters]', your_string))

# Example usage:
# say you want to count all G, g, C, or c characters in the following DNA seq
len(re.findall('[GgCc]', "ACGTGCAcgattcgatCGCTAGCTAG"))
--> 14

Comments

Submit
0 Comments