I’m trying to extract everything before the text becomes uppercase at the letter G, and none of the text that comes after.
string = 'abcdefGfecdab'
So the goal is to return ‘abcdef’.
I tried a for loop with a nested while loop
for a in string:
while(a==a.lower()):
print(a)
>Solution :
Don’t print, concatenate to a result string that you’ll return at the end.
You don’t need a while loop, you need an if statement to test the current character. And when the test fails, use break to exit the for loop.
result = ''
for a in string:
if a.islower():
result += a
else:
break
print(result)