Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read from string until string becomes uppercase?

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading