import re
s = 'start foo end'
s = re.sub('foo', 'replaced', s)
s
s = re.sub('(?<= )(.+)(?= )', lambda m: 
    'can use a callable for the %s text too' % m.group(1), s)
s
help(re.sub)
Regex replace (in Python) - a simpler way?