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>You want <code>return</code> to be same as <code>print</code>, <code>return</code> switches context from <code>callee</code> to <code>caller</code> function, with some value or none.
Whereas <code>print</code> is used to send some output to <code>console</code></p>

<p>They both cannot be same.</p>

<p>In your case, assuming that you want to write this code snippet as a function and return values that are being printed, it should be done as follows:</p>

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

python