Print comma separated items without newline
<p>I thought I was copying word for word from a <a href="http://stackoverflow.com/questions/493386/how-to-print-in-python-without-newline-or-space">previous question/answer</a>, but, I don't get what I wanted. </p>

<p>I was trying to answer a question from an online list of python questions for beginners. I wrote the following in a file called <code>q.py</code> and then gave the executed it with <code>python3 q.py</code> in my shell.</p>

<p>The file <code>q.py</code> looks like:</p>

<pre><code>from math import sqrt
C=50
H=30
inp = input('input values:   ')
vals = inp.split(',')
print(vals)
for x in vals:
    D=int(x)
    print(int(sqrt(2*C*D/H)),sep=',',end='')
print()
</code></pre>

<p>I input <code>100,150,180</code> as the question suggested, the required answer is:</p>

<pre><code>18,22,24 
</code></pre>

<p>but the answer I get is:</p>

<pre><code>182224
</code></pre>

<p>When I omit the <code>"end"</code> argument to <code>print</code>, I get <code>18</code>, <code>22</code>, and <code>24</code> separated by newlines.</p>

<p><code>sep</code> is used to separate values <em>if multiple values are actually provided</em>; you provide <em>one value</em> for every loop so the separator isn't used. </p>

<p>Instead of using <code>sep=','</code>, use <code>end=','</code> to get the desired effect:</p>

<pre><code>for x in vals:
    D=int(x)
    print(int(sqrt(2*C*D/H)),end=',')
print()
</code></pre>

<p>Using similar input, the result printed out is:</p>

<pre><code>['100', '150', '180']
18,22,24,
</code></pre>

<p>If, for example, you provided <em>multiple</em> values, say, by unpacking a list comprehension in your <code>print</code> call, <code>sep=','</code> would work like a charm:</p>

<pre><code>print(*[int(sqrt(2*C*int(x)/H)) for x in vals], sep=',')
</code></pre>

<p>Prints:</p>

<pre><code>18,22,24
</code></pre>

python python-3.x printing