I have a python string received from user input.
Let's say the user input is something like this:
I am Enrolled in a course, 'MPhil' since 2014. I LOVE this 'SO MuCH'
If this string is stored in a variable called input_string, and I apply .lower() on it, it would convert the whole thing into lowercase.
input_string = input_string.lower()
Result:
i am enrolled in a course, 'mphil' since 2014. i love this 'so much'
Here's what I desire the lowercase to do: Convert everything, except what's in quotes, into lowercase.
i am enrolled in a course, 'MPhil' since 2014. i love this 'SO MuCH'
We can use the combination of negative lookahead, lookbehind, applying word boundaries and using a replacement function:
>>> s = "I am Enrolled in a course, 'MPhil' since 2014. I LOVE this 'SO MuCH'"
>>> re.sub(r"\b(?<!')(\w+)(?!')\b", lambda match: match.group(1).lower(), s)
"i am enrolled in a course, 'MPhil' since 2014. i love this 'SO MuCH'"