string reversal function using reversed indexing
<p>Could someone please help me understand why the following function does not print out the reverse of the string? What am I doing wrong?</p>

<pre><code>def myReverse(data):
    for index in range( len(data)-1, -1, -1 ):
        return data[index]
        print( data[index] )

myReverse('blahblah')
</code></pre>

<p>When you make a <code>return</code> call within the function, the control comes back to parent (which executed the function) ignoring the code within the scope of function after <code>return</code>. In your case, that is why <code>print</code> is not getting executed by your code.</p>

<p>Move the line containing <code>print</code> before <code>return</code> and move <code>return</code> to outside of the <code>for</code> loop. Your code should work then.</p>

<p><strong>Suggestion:</strong>
There is simpler way to reverse the string using <code>::-1</code>. For example:</p>

<pre><code>&gt;&gt;&gt; my_string = 'HELLO'
&gt;&gt;&gt; my_string[::-1]
'OLLEH'
</code></pre>

python