Sorry if this has been asked before, but I've been struggling with this for weeks now.
I'm trying to figure out how to replace a particular substring within an arbitrary string with another particular substring.
IE:
If I get user to type in an arbitrary string, I want to replace any instance of "MAA" with something like "UOP".
My code:
cstring = input("Enter a character string: ")
for i in cstring:
if ((i != 'M') and (i != 'U') and (i != 'I')):
break
else:
if (cstring[-1] == 'I'):
cstring = cstring + 'U'
if (cstring[:1] == 'M'):
cstring = cstring + cstring[1:]
if ('III' in cstring):
cstring = cstring[:i] + "U" + cstring[i:]
if ('UU' in cstring):
cstring = cstring[:i] + '' + cstring[i:]
break
print(cstring)
Thanks!