регулярное выражение может сделать это с легкостью:
import re
regx = re.compile('at\\b')
ch = 'the fat cat was impressed by all the rats gathering at one corner of the great room'
print ch
print
print regx.sub('ATU',ch)
результат
the fat cat was impressed by all the rats gathering at one corner of the great room
the fATU cATU was impressed by all the rats gathering ATU one corner of the greATU room
.
PS
С помощью регулярных выражений мы можем выполнять очень сложные задачи.
Например, замена нескольких типов строк определенной заменой для каждой, благодаря использованию функции обратного вызова (здесь она называется repl , которая получает перехваченные объекты MatchObjects)
import re
regx = re.compile('(at\\b)|([^ ]r(?! ))')
def repl(mat, dic = {1:'ATU',2:'XIXI'}):
return dic[mat.lastindex]
ch = 'the fat cat was impressed by all the rats gathering at one corner of the great room'
print ch
print
print regx.sub(repl,ch)
результат
the fat cat was impressed by all the rats gathering at one corner of the great room
the fATU cATU was imXIXIessed by all the rats gathXIXIing ATU one cXIXIner of the XIXIeATU room