You don't need two loops.
First, let's put all the digits in a list:
>>> a = list(map(int, str(5**150000)))
Then calculate the sum of the first 50000 digits:
>>> maximum = current = sum(a[:50000])
>>> current
225318
Now, let's loop through the list, removing the lowest digit from the sum and adding the next one 50000 digits ahead during each iteration:
>>> for i in range(0, len(a)-50000):
... current = current - a[i] + a[i+50000]
Check if that new sum is larger than the previous one, and if so, make it the new "interim maximum":
... if current > maximum: maximum = current
...
Once the loop exits, maximum
contains the maximum value:
>>> maximum
225621
Let's put it all into a function, so no copying mistakes occur:
def maxdigitsum(number, digits):
l = list(map(int, str(number)))
maximum = current = sum(l[:digits])
for i in range(0, len(l)-digits):
current = current - l[i] + l[i+digits]
if current > maximum: maximum = current
return maximum