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>I think this will work , Solution in Python 2.7.6: </p>

<pre><code>def print_list(b):
    count = 0
    width = 2
    for k in range(0, len(b), width):
        yield b[k:k + width]
b = 'abbaab'
l = list(print_list(b))
for i in l:
    print i 
</code></pre>

<p><strong>Output</strong>:</p>

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

<p>Here I am using <code>yield</code> statement to instead of <code>return</code> to return the every line element and trying to convert all into <code>list</code> and every element in list represent each line Or else it is not necessary to convert it into <code>list</code> you can use this:</p>

<pre><code> for i in print_list(b):
    print i 
</code></pre>

<p>If you want to use <code>return</code> then Solution :</p>

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

b = 'abbaab'
for i in print_list(b):
    print i
</code></pre>

<p>Hope this helps.</p>

python