0

So I have a little problem, I want to count how many times a string : "aa" is in my longer string "aaatattgg" its looks like a dna sequence. Here for exemple I expect 2 (overlap is allow) There is the .count method but overlap is not allowed PS: excuse my english , I'm french

M.javid
  • 6,387
  • 3
  • 41
  • 56
LBK
  • 21
  • 2

1 Answers1

1

Through re module. Put your regex inside positive lookarounds in-order to do overlapping match.

>>> import re
>>> s = "aaatattgg"
>>> re.findall(r'(?=(aa))', s)
['aa', 'aa']
>>> len(re.findall(r'(?=(aa))', s))
2
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274