如何在Python中有条件地替换字符串?

spr*_*ssd 0 python

我需要在字符串中查找子字符串,如果找不到,请执行操作.所以我这样做,它工作正常:

if 'AAA' not in myString: 
   print "AAA is not in myString"
Run Code Online (Sandbox Code Playgroud)

但是,我需要避免AAA是XAAA的一部分.换句话说,我不希望我的脚本注意到任何"XAAA"的存在 if 'AAA' not in myString.那么如何将上面的if子句修改为以下内容:

if 'AAA' not in myString except for cases where 'AAA' is a part of 'XAAA':
    print "AAA is not in myString"
Run Code Online (Sandbox Code Playgroud)

Gri*_*lis 7

我喜欢重新模块解决方案

if not re.search(r'(?<!X)AAA', myString):
    ...
Run Code Online (Sandbox Code Playgroud)