How to use multiple 'if' statements nested inside an enumerator?
<p>I have a massive string of letters all jumbled up, 1.2k lines long.
I'm trying to find a lowercase letter that has EXACTLY three capital letters on either side of it.</p>

<p>This is what I have so far</p>

<pre><code>def scramble(sentence):
    try:
        for i,v in enumerate(sentence):
            if v.islower():
                if sentence[i-4].islower() and sentence[i+4].islower():
    ....
    ....
    except IndexError:
        print() #Trying to deal with the problem of reaching the end of the list


#This section is checking if 
the fourth letters before 
and after i are lowercase to ensure the central lower case letter has
exactly three upper case letters around it
</code></pre>

<p>But now I am stuck with the next step. What I would like to achieve is create a <code>for-loop</code> in range of <code>(-3,4)</code> and check that each of these letters is uppercase. If in fact there are three uppercase letters either side of the lowercase letter then print this out.</p>

<p>For example</p>

<pre><code>for j in range(-3,4):
    if j != 0:
        #Some code to check if the letters in this range are uppercase
        #if j != 0 is there because we already know it is lowercase
        #because of the previous if v.islower(): statement.
</code></pre>

<p>If this doesn't make sense, this would be an example output if the code worked as expected</p>

<pre><code>scramble("abcdEFGhIJKlmnop")
</code></pre>

<p>OUTPUT</p>

<pre><code>EFGhIJK
</code></pre>

<p>One lowercase letter with three uppercase letters either side of it.</p>

<p>regex is probably the easiest, using a modified version of @Israel Unterman's answer to account for the outside edges and non-upper surroundings the full regex might be:</p>

<pre><code>s = 'abcdEFGhIJKlmnopABCdEFGGIddFFansTBDgRRQ'

import re
words = re.findall(r'(?:^|[^A-Z])([A-Z]{3}[a-z][A-Z]{3})(?:[^A-Z]|$)', s)

# words is ['EFGhIJK', 'TBDgRRQ']
</code></pre>

<p>using <code>(?:.)</code> groups keeps the search for beginning of line or non-upper from being included in match groups, leaving only the desired tokens in the result list.  This should account for all conditions listed by OP.</p>

<p><em>(removed all my prior code as it was generally *bad*)</em></p>

python