Repeat a string n times and print n lines of it
<p>i've been stuck on a question for some time now:</p>

<p>I'm looking to create a python function that consumes a string and a positive integer. The function will print the string n times, for n lines. I <strong>cannot</strong> use loops, i must only use recursion</p>

<p>e.g.</p>

<pre><code>repeat("hello", 3)

hellohellohello
hellohellohello
hellohellohello
</code></pre>

<p>whenever i try to make a function that does this, the function decreases the length of the string, progressively:</p>

<p>e.g.</p>

<pre><code>repeat("hello", 3)

hellohellohello
hellohello
hello
</code></pre>

<p>here's what my code looks like:</p>

<pre><code>def repeat(a, n):
if n == 0:
    print(a*n)
else:
    print(a*n)
    repeat(a, n-1)
</code></pre>

<p>any help would be appreciated, thanks!</p>

<p>One liner</p>

<pre><code>def repeat(a,n):
    print((((a*n)+'\n')*n)[:-1])
</code></pre>

<p>Let's split this up</p>

<ol>
<li><code>a*n</code>  repeats string <code>n</code> times, which is what you want in one line</li>
<li><code>+'\n'</code> adds a new line to the string so that you can go to the next line</li>
<li><code>*n</code> because you need to repeat it <code>n</code> times</li>
<li>the <code>[:-1]</code> is to remove the last <code>\n</code> as <code>print</code> puts a new-line by default.  </li>
</ol>

python recursion