0

How to write a Python program to find the largest substring of uppercase characters and print the length of that substring? Sample Input - I lovE PRogrAMMING Sample Output - 6 Explanation - AMMING is the largest substring with all characters in uppercase continuously.

stuck with this for more than an hour and still no clue :(

created a list out of the string by using the split method and no clue on what's next.

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
Joe
  • 31
  • 3
  • Welcome to SO! What exactly have you tried so far? We're much more here to help with specific questions of the form "I tried X, but it did not do what I expect and instead resulted in an error!" accompanied by a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) .. that said you may find a form like [`for index, character in enumerate(some_string):`](https://docs.python.org/3/library/functions.html#enumerate) is useful to individually check each character while knowing where it is in the string, and slice notation to take blocks https://stackoverflow.com/a/509295/ – ti7 Mar 05 '23 at 18:03

2 Answers2

0
import re
s='I lovE PRogrAMMING '
upper_case_letters=re.findall(r"[A-Z]+", s)

print(upper_case_letters)
#['I', 'E', 'PR', 'AMMING']

print(max(upper_case_letters,key=len))
#output
'AMMING'

print(len(max(upper_case_letters,key=len)))
#6
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
0

You can use itertools.groupby for the task:

from itertools import groupby

s  ='I lovE PRogrAMMING'

out = max(sum(1 for _ in g) for k, g in groupby(s, str.isupper) if k)
print(out)

Prints:

6
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91