0

How can I separate a string irregardless of upper or lower case?

line = 'Thing Replaces OtherThing'
before, sep, after = line.partition(' replaces ')
print(before)
print(after)

I expect the output to be "Thing" and "OtherThing".

Edit: I also need it to work with no spaces in the string

line = 'ThingReplacesOtherThing'
before, sep, after = line.partition('replaces')

user2820068
  • 69
  • 1
  • 5
  • You've sadly exhausted the reasonable use cases for plain `str` methods, so you'll want a regex, which the duplicate covers. It's *technically* possible to do with `str` methods, but so ugly I'd prefer the regex. The ugly solution would be: `beforelen, _, afterlen = map(len, line.casefold().partition(' replaces '))`, then slice the original `line` based on those partitioned component lengths, e.g. following up with `before, sep, after = line[:beforelen], line[beforelen:-afterlen], line[-afterlen:]`), but I can't *recommend* tricky code like that in good conscience. – ShadowRanger May 27 '22 at 12:34
  • Just for fun, I did [post this solution to the duplicate](https://stackoverflow.com/a/72405738/364696). I don't expect any up-votes for it. :-) – ShadowRanger May 27 '22 at 12:52

0 Answers0