I am given the sentence My team is the BeST team at the tournament. as a test case to create create a function
def case_insensitivity(sentence):
return new_sentence
which checks a given sentence for a case insensitive word and replaces the case insensitive word with XXX_.
i.e.
print(case_insensitivity('My team is the BeST team at the tournament'))
result
'My team is the XXX_ team at the tournament'
What’s the best way to do this? casefold()
>Solution :
Assuming we define the target words as having both a lowercase and uppercase beyond the first character, we can try using re.sub for a regex option:
def case_insensitivity(sentence):
return re.sub(r'\w+(?:[A-Z]\w*[a-z]|[a-z]\w*[A-Z])\w*', 'XXX_', sentence)
print(case_insensitivity('My team is the BeST team at the tournament'))
This prints:
My team is the XXX_ team at the tournament