From the code it is obvious that the case template for the replacement is made from the case match template, repeating it (like StufF -> BanaNA ). Given this, I will first find the case pattern for the entire string, and then return the string to the desired case:
def to_case(s, cmap): 'returns string cased according to map' return ''.join([c.upper() if m else c for (c,m) in zip(s,cmap)]) input_str = "myStrIngFullOfStUfFiWannAReplaCE_StUfFs" replace_str = "stuff" replacer_str = "banana" case_map = [c.istitle() for c in input_str]
Andrey Sobolev
source share