When printing outputs an empty line appears before my outputs
<p>I have attempted to write a program which asks the user for a string and a number (On the same line) and then prints all possible combinations of the string up to the size of the number. The output format should be: All capitals, Each combination on each line, Length of combination(Shortest First) and in alphabetical.</p>

<p>My code outputs the right combinations in the right order but it places an empty before the outputs and I'm not sure why.</p>

<pre><code>from itertools import combinations
allcombo = []
S = input().strip()
inputlist = S.split()
k = int(inputlist[1])
S = inputlist[0]

#
for L in range(0, k+1):
    allcombo = []

    for pos in combinations(S, L):

        pos = sorted(pos)
        pos = str(pos).translate({ord(c): None for c in "[]()', "})
        allcombo.append(pos) 
        allcombo = sorted(allcombo)  
    print(*allcombo, sep = '\n')
</code></pre>

<p>Input:</p>

<pre><code>HACK 2
</code></pre>

<p>Output:</p>

<pre><code>(Empty Line)
A
C
H
K
AC
AH
AK
CH
CK
HK
</code></pre>

<p>Also I've only been coding for about a week so if anyone would like to show me how to write this properly, I'd be very pleased.</p>

<p>Observe the line:</p>

<pre><code>for L in range(0, k+1) # Notice that L is starting at 0.
</code></pre>

<p>Now, observe this line:</p>

<pre><code>for pos in combinations(S, L)
</code></pre>

<p>So, we will have the following during our first iteration of the inner for loop:</p>

<pre><code>for pos in combinations(S, 0) # This is an empty collection during your first loop.
</code></pre>

<p>Basically no work is being performed inside your loop because there is nothing to iterate over, and you will just being printing an empty string.</p>

<p>Change the following code:</p>

<pre><code>for L in range(0, k+1)
</code></pre>

<p>to this:</p>

<pre><code>for L in range(1, k+1) # Skips the empty collection since L starts at 1.
</code></pre>

<p>and this will fix your problem.</p>

python combinations