if c.isupper():
    result += new[i].upper()
else:
    result += new[i].lower()
all_upper = False
if all_upper:
    result += new[i + 1:].upper()
else:
    result += new[i + 1:].lower()
return result
regex = re.compile(re.escape(old), re.I)
return regex.sub(repl, string)
print(case_sensitive_replace('abc Abc aBc abC ABC', 'abc', 'de'))
print(case_sensitive_replace('abc Abc aBc abC ABC', 'abc', 'def'))
print(case_sensitive_replace('abc Abc aBc abC ABC', 'abc', 'defg'))
case sensitive string replacement in Python