For loop not working without range() " string indices must be integers"
<p><code>i</code> is a string, so how can I make this work? </p>

<p>How can I use <code>i</code> as index?</p>

<pre><code>for i in s[1:]:
    if s[i] &lt;= s[i-1]:
        temp += s[i]
    else:
        subs.append(temp)
        temp = ''
</code></pre>

<p>I've tried to use </p>

<pre><code>for i in s[1:]:
    if s.index(i) &gt;= s.index(i-1):
        temp += s[i]
    else:
        subs.append(temp)
        temp = ''
</code></pre>

<p>And I get</p>

<blockquote>
  <p>TypeError: unsupported operand type(s) for -: 'str' and 'int'</p>
</blockquote>

<p>the syntax you are using is corresponding to string iteration (see <a href="http://anandology.com/python-practice-book/iterators.html" rel="nofollow">http://anandology.com/python-practice-book/iterators.html</a>) so the <code>i</code> will iterate on the different characters of the string on note on their indices:</p>

<pre><code>&gt;&gt;&gt; for i in "hello"[1:]:
...   print i
... 
e
l
l
o
</code></pre>

<p><del>in the initial faulty loop, note that access to respectively <code>s[i+1]</code> or <code>s[i-1]</code> are incorrect when <code>i</code> is pointing to respectively last and first character of the string.</del> (question modified)</p>

<p>To iterate on the indices, use xrange (for instance between second and the one before the last character):</p>

<pre><code>s='hello'
for i in xrange(1,len(s)-1):
   print i,s[i]
</code></pre>

<p>Generates:</p>

<pre><code>e
l
l
</code></pre>

python for-loop