Is there any way to print **kwargs in Python
<p>I am just curious about <code>**kwargs</code>. I am just started learning it, So while going through all the question on stackoverflow and video tutorials I notice we can do like this </p>

<pre><code>def print_dict(**kwargs):
    print(kwargs)
print_dict(x=1,y=2,z=3)  
</code></pre>

<p>Which gives output as :<code>{'y': 2, 'x': 1, 'z': 3}</code>
So I figures why not do the reverse and print the like <code>x=1,y=2,z=3</code></p>

<p>So I tried this:</p>

<pre><code>mydict = {'x':1,'y':2,'z':3}
print(**mydict)
</code></pre>

<p>But I got an error like : <code>TypeError: 'z' is an invalid keyword argument for this function</code>(sometimes it shows 'y' is an invalid keyword).</p>

<p>I also tried like assign it to the variable and then print it but again i got an error (<code>SyntaxError: invalid syntax</code>):</p>

<pre><code>mydict = {'x':1,'y':2,'z':3}
var = **mydict
print(var)
</code></pre>

<p>See this is working:</p>

<pre><code>def print_dict(**this):
    print(this)
mydict = {'x':1,'y':2,'z':3}
print_dict(**mydict)
</code></pre>

<p>But instead of <code>print(this)</code> if i do <code>print(**this)</code> it gives the error.</p>

<p>As we can print <code>*arg</code> as I try this code:</p>

<pre><code>def num(*tuple_num):
    print(tuple_num)
    print(*tuple_num)
num(1,2,3,4)
</code></pre>

<p>It run perfectly and gives output as:</p>

<pre><code>(1, 2, 3, 4)
1 2 3 4
</code></pre>

<p>So I want to know is there any possible solution/way to print <code>**kwargs</code> ?</p>

<p>The syntax <code>callable(**dictionary)</code> <em>applies</em> the dictionary as if you used separate keyword arguments.</p>

<p>So your example:</p>

<pre><code>mydict = {'x':1,'y':2,'z':3}
print(**mydict)
</code></pre>

<p>Is internally translated to:</p>

<pre><code>print(x=1, y=2, z=3)
</code></pre>

<p>where the exact ordering depends on the current random hash seed. Since <code>print()</code> doesn't support those keyword arguments the call fails.</p>

<p>The other <code>print()</code> call succeeds, because you passed in the values as separate <em>positional</em> arguments:</p>

<pre><code>tuple_num = (1, 2, 3, 4)
print(*tuple_num)
</code></pre>

<p>is effectively the same as:</p>

<pre><code>print(1, 2, 3, 4)
</code></pre>

<p>and the <code>print()</code> function supports separate arguments by writing them out one by one with the <code>sep</code> value in between (which is a space by default).</p>

<p>The <code>**dictionary</code> is not valid syntax outside of a call. Since <code>callable(**dictionary)</code> is part of the call syntax, and not an object, there is <em>nothing to print</em>.</p>

<p>At most, you can <em>format</em> the dictionary to look like the call:</p>

<pre><code>print(', '.join(['{}={!r}'.format(k, v) for k, v in mydict.items()]))
</code></pre>

python