s = "aB:cD"
def transform_string():
string_Length = len(s)
pos_colon = s.find(":")
lowerCase = s.lower()
upperCase = s.upper()
string_partA = 0 + pos_colon
res = lowerCase[:string_partA] + upperCase[string_partA:]
return res
print(transform_string())
print()
The code is meant to have the part of the string before the colon lowercased and after the colon uppercased. So my question is what is
res = lowerCase[:string_partA] + upperCase[string_partA:]
the bracket in there is meant to be for, or more explicitly, when can we use that? Does that syntax have a name? I saw someone else use it and I could logically follow how it works and what it does, I just wonder if that has a name or syntax restrictions and so on...
I first tried to use a for loop (would appreciate if someone could tell me why that is wrong, I know it's not as efficient as the code above):
s = "aB:cD"
def transform_string():
y = len(s)
x = s.find(":")
for s in range(0, x):
first_half = s.lower()
for s in range(x, y):
second_half = s.upper()
res = f"{first_half}+{second_half}"
return res
print(transform_string())
print()
Thanks!