How to make return do the same as print in python string
<pre><code>b = 'abbaab'
count = 0
width = 2
for k in range(0, len(b), width):
    print b[k:k + width]
</code></pre>

<p>gives me</p>

<pre><code> ab
 ba
 ab
</code></pre>

<p>but I need to use return instead of print. I don't know how to store each line into something, the things I tried said index out of range.</p>

<p><code>print</code> and <code>return</code> are very different.</p>

<p>Print is a function that will just print out something to the screen. return is a command that basically tells what value a function should return. For example:</p>

<pre><code>def func1():
    print 1

def func2()
    return 2
</code></pre>

<p>if you do</p>

<pre><code>first_result = func1()
second_result = func2()
</code></pre>

<p>you will see that</p>

<pre><code>first_result = None
second_result = 2
</code></pre>

<p>but if you run them</p>

<pre><code>func1()
func2()
</code></pre>

<p>you'll see that only <code>1</code> will be printed to the screen. In order to print the return of a function, you have to explictly ask for it</p>

<pre><code>print func2()
</code></pre>

<p>With that in mind, and heading to your problem, you can store your things and return that to the function, and just then ask for python to print everything out</p>

<pre><code>def func3():
    b = 'abbaab'
    count = 0
    width = 2
    things = []
    for k in range(0, len(b), width):
        things.append(b[k:k + width])
    return things

print func3()
</code></pre>

<p>Of course in this csae, you will print all values at once. In order to print each line separately, iterate the result and print each element</p>

<pre><code>value_returned = func3()
for element in value_returned:
    print element
</code></pre>

python