Writing query
inside a string doesn't stores the value of query
inside that string, it simply creates a new string with the string 'query' in it.
>>> print '\w+(query)\w+'
\w+(query)\w+
You should use string formatting:
>>> sequence = 'abcdefghijklmnopqrstuvwxyz'
>>> query = 'jklmnop'
>>> '(\w+)({})(\w+)'.format(query)
'(\\w+)(jklmnop)(\\w+)'
>>> re.search('(\w+)({})(\w+)'.format(query),sequence).group(1)
'abcdefghi'
>>> re.search('(\w+)({})(\w+)'.format(query),sequence).group(3)
'qrstuvwxyz'
It's better to use re.split
for such purposes:
>>> strs = 'abcdefghijklmnopqrstuvwxyz'
>>> import re
>>> before, after = re.split('jklmnop',strs)
>>> before
'abcdefghi'
>>> after
'qrstuvwxyz'