using a nested if statement within a for loop
<p>i'm trying to read through a string (with no spaces) pull out instances where there is a single lowercase letter surrounded on both sides by 3 upper cases (i.e. HHSkSIO). I've written the code below:</p>

<pre><code>def window(fseq, window_size=7):
    for i in xrange(len(fseq) - window_size + 1):
        yield fseq[i:i+window_size]


for seq in window('asjdjdfkjdhfkdjhsdfkjsdHJJnJSSsdjkdsad', 7):
    if seq[0].isupper and seq[1].isupper and seq[2].isupper and seq[3].islower and seq[4].isupper and seq[5].isupper and seq[6].isupper:
            print seq
</code></pre>

<p>where the first function window allows me to iterate through the string using a sliding window of 7 and the second part, the for statement, checks whether the characters within each window are higher higher higher lower higher higher higher. When I run the code, it comes out with:</p>

<pre><code>asjdjdf
sjdjdfk
jdjdfkj
djdfkjd
jdfkjdh
dfkjdhf
fkjdhfk
kjdhfkd
jdhfkdj
dhfkdjh
hfkdjhs
fkdjhsd
kdjhsdf
djhsdfk
jhsdfkj
hsdfkjs
sdfkjsd
dfkjsdH
fkjsdHJ
kjsdHJJ
jsdHJJn
sdHJJnJ
dHJJnJs
HJJnJsd
JJnJsdj
JnJsdjk
nJsdjkd
Jsdjkds
sdjkdsa
djkdsad
</code></pre>

<p>How can I make the for statement <strong>only</strong> print out the sliding window which conforms to the above if statement, rather than printing out all of them? P.S i know this is probably a very clunky way of doing it, I'm a beginner and it was the only thing i could think of!</p>

<p>The problem is that you are missing the () in your calls to .isupper, which always evaluate to true.</p>

<p>Try:</p>

<pre><code>def window(fseq, window_size=7):
    for i in range(len(fseq) - window_size + 1):
        yield fseq[i:i+window_size]


for seq in window('asjdjdfkjdhfkdjhsdfkjsdHJJnJSSsdjkdsad', 7):
    if seq[0].isupper() and seq[1].isupper() and seq[2].isupper() and seq[3].islower() and seq[4].isupper() and seq[5].isupper() and seq[6].isupper():
        print (seq)
</code></pre>

<p>The other way of doing it, would be:</p>

<pre><code>import re
s = re.compile(r'[A-Z]{3}[a-z][A-Z]{3}')
def window(fseq, window_size=7):
    for i in range(len(fseq) - window_size + 1):
        yield fseq[i:i+window_size]

for seq in window('asjdjdfkjdhfkdjhsdfkjsdHJJnJSSsdjkdsad', 7):
    result = s.search(seq)
    if result is not None:
        print(result.group())
</code></pre>

python string iteration