<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Nov 26, 2013 at 4:01 PM, Victor Hooi <span dir="ltr"><<a href="mailto:victorhooi@gmail.com" target="_blank">victorhooi@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi,<br>
<br>
I'm trying to use Python's new style string formatting with a dict and string together.<br>
<br>
For example, I have the following dict and string variable:<br>
<br>
    my_dict = { 'cat': 'ernie', 'dog': 'spot' }<br>
    foo = 'lorem ipsum'<br>
<br>
If I want to just use the dict, it all works fine:<br>
<br>
    '{cat} and {dog}'.format(**my_dict)<br>
    'ernie and spot'<br>
<br>
(I'm also curious how the above ** works in this case).<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<br>
However, if I try to combine them:<br>
<br>
    '{cat} and {dog}, {}'.format(**my_dict, foo)<br>
    ...<br>
    SyntaxError: invalid syntax<br></blockquote><div><br></div><div>Here you almost have it right. If you flip the arguments around to look like:</div><div>    '{cat} and {dog}, {}'.format(foo, **my_dict)<br></div>

<div>it will work as you expect.</div><div><br></div><div>The issue is that you cannot specify positional arguments (foo) after keyword arguments (**my_dict).</div><div><br></div><div>In the code you tried, what Python is doing is:</div>

<div>    '{cat} and {dog}, {}'.format(cat=ernie, dog=spot, foo)<br></div><div>which, if tried, provides the nicer error message of "SyntaxError: non-keyword arg after keyword arg".</div></div></div></div>