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>You are returning before print. So the line <code>print( data[index] )</code> is never get executed. </p>

<p>Below code wil work just fine. </p>

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

<p>Notice that this is a python3 solution. <code>print</code> in Python2 doesn't work like that. </p>

<p>So, get the same effect in python2 , first import the <code>print</code> from <code>__future__</code>.</p>

<pre><code>from __future__ import print_function
</code></pre>

<p>And then, same as above. </p>

python