-2

I am looking for the following:

dna = '**atg**abcdefghijk**tga**aa'

start = 'atg'

stop = 'tag' or 'taa' or 'tga'

I would like to get all the characters between start and stop. I have tried with:

print (dna.find(start:stop))

but it keeps me saying that the ":" are invalid syntax.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • Well, yeah. That's not how you call find. Also, I don't think find does what you think it does. I suspect you want slicing. – Morgan Thrapp Nov 18 '15 at 17:54
  • 1
    Possible duplicate of [Find string between two substrings](https://stackoverflow.com/questions/3368969/find-string-between-two-substrings) – narendra-choudhary Nov 18 '15 at 17:55

1 Answers1

1

You could use a regular expression to help find any suitable matches as follows:

import re

dna = 'atgabcdefghijktgaaa'
start = 'atg'
stop = ['tag', 'taa', 'tga']

print re.findall(r'{}(.*?)(?:{})'.format(start, '|'.join(stop)), dna)

This would display the following:

['abcdefghijk']
Martin Evans
  • 45,791
  • 17
  • 81
  • 97