d = {'bean': 'robert', 'beans': 'cars'}
str_in = 'bean likes to sell his beans'
str_out = re.sub('\\b(\\w+)\\b', lambda m: d.get(m.group(1), m.group(1)),
    str_in)
Replace all the occurrences of specific words