I open this question although a duplicate of it exists in C# lang. (without the relevant resolution needed).
Im trying to replace all the lower case characters in a given string, with upper case chars, and vice versa. This should be done simultaneously with the least time complexity (because of use in high volume of verbal translations).
The IO:
input: str_1 = "Www.GooGle.com"
output: "wWW.gOOgLE.COM"
The code:
import re # import RegEx lib
str_1 = "Www.GooGle.com" # variable tested
def swapLowerUpper(source):
# takes source string
# returns group 0 regex to lower and group 1 regex to upper, by source
return re.sub(r'([A-Z]+)|([a-z]+)', lambda x: x.group(0).lower(), source)
# check the output
print(swapLowerUpper(str_1)
The Question:
I have a hard time in triggering the second group (which index is 1) and apply the attribute ".upper()" on it. My attempt was to open it as {x: x.group(0).lower(), x: x.group(1).upper()} which failed.