This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients lys.nikolaou, nimit.grover24, rhettinger
Date 2021-07-29.21:29:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1627594178.49.0.748746636724.issue44773@roundup.psfhosted.org>
In-reply-to
Content
I don't think this should be done.  

If case doesn't matter at all, the input can be casefolded before the replacement:

  s.casefold().replace('hippo', 'giraffe').  

If it can't be casefolded in advance because the case actually matters, then ​it doesn't make sense to mix a case-insensitive search step with a case-sensitive replacement.

Presumably if case matters at all in the original string, then the new text would need to match the case of the old text.  With the example in the selected StackOverflow answer, we get undesirable output for most of the case variants:

 ​strings = [
    ​'I WANT A HIPPO FOR MY BIRTHDAY!',  # lowercase giraffe doesn't fit
    ​'I want a hippo for my birthday.',  # only makes sense when the case matches
    ​'I Want A Hippo for My Birthday',   # lowercase giraffe doesn't fit
    ​'I want a hIPpo for my birthday',   # desired outcome unknown
 ​]
 ​for s in strings:
     ​print(s.replace('hippo', 'giraffe', case_insensitive=True)

ISTM that every answer in the StackOverflow entry has only a toy examples and wouldn't make sense for real text where case is retained everywhere except for the substitutions.
History
Date User Action Args
2021-07-29 21:29:38rhettingersetrecipients: + rhettinger, lys.nikolaou, nimit.grover24
2021-07-29 21:29:38rhettingersetmessageid: <1627594178.49.0.748746636724.issue44773@roundup.psfhosted.org>
2021-07-29 21:29:38rhettingerlinkissue44773 messages
2021-07-29 21:29:38rhettingercreate