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>You have to build your own formatting for dictionaries (nothing else are <code>**kwargs</code>):</p>

<pre><code>print(','.join('{0}={1!r}'.format(k,v) for k,v in this.items()))
</code></pre>

python