Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'subject:Python': 0.06; 'subject:both': 0.07; 'string': 0.09; 'arguments': 0.09; 'formatting': 0.09; 'subject:()': 0.09; 'subject:string': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; "(i'm": 0.16; 'arg': 0.16; 'dict': 0.16; 'positional': 0.16; 'subject:Formatting': 0.16; 'subject:String': 0.16; 'syntaxerror:': 0.16; 'which,': 0.16; 'wrote:': 0.18; 'trying': 0.19; "python's": 0.19; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'skip:{ 20': 0.24; 'specify': 0.24; 'subject: .': 0.24; 'together.': 0.24; 'cc:2**0': 0.24; 'right.': 0.26; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'style': 0.33; 'received:209.85': 0.35; 'received:google.com': 0.35; 'curious': 0.36; 'keyword': 0.36; 'doing': 0.36; 'hi,': 0.36; 'example,': 0.37; 'received:209': 0.37; 'skip:& 10': 0.38; 'nov': 0.38; 'issue': 0.38; 'pm,': 0.38; 'how': 0.40; 'new': 0.61; 'here': 0.66; '26,': 0.68; 'invalid': 0.68; 'dict,': 0.84; 'victor': 0.84; 'to:none': 0.92; '2013': 0.98 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc:content-type; bh=/fjZoZJZlmb7HsqJ1WolpjWxGWOQC+hoLblA6qDFL1Q=; b=kSp+sqk6HJ5D/Xx1uZzQuhPtzmFlWcZpWJBSENvKqaNe4Z6oNe2EQz7tYsVOlRq2Cn YqDRGN0EVkE3FU0h2XKNgkNUMEw0JfCkS0EkRl7yMrA+A7LF23KjRiO9E6XvK7YZO900 pTrSUcHwGfYYjc//zqb4Kw40muP8jXMJGC+vPgEIjYhAq/Rf+nqLGymuYuaSIjSGnxMu ayEogaQtSlhvsqLSh9zGlz2jwv0OLD1jVHwZgpmv/0VGRRV2bv4P1I8Oi0On/iaj1cRu 8dq2Uoq25dpxnPGbBSvQ8jmCIwxw5ouZDh6gH6wYNu5gSLHjjAvedhVs0Mz084I0QAL2 1nqg== X-Gm-Message-State: ALoCoQnfdGNaqaXybIythvPRmaD49gRPHHdjtJ1kwEc6/Vco1GslV4Wd/yp4VoSHy4VTykhHKlLp X-Received: by 10.60.37.33 with SMTP id v1mr11257086oej.2.1385511708395; Tue, 26 Nov 2013 16:21:48 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Chris Kaynor Date: Tue, 26 Nov 2013 16:21:27 -0800 Subject: Re: Python String Formatting - passing both a dict and string to .format() Cc: "python-list@python.org" Content-Type: multipart/alternative; boundary=089e0117691f36201304ec1d95a0 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 97 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385511717 news.xs4all.nl 15883 [2001:888:2000:d::a6]:46229 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60559 --089e0117691f36201304ec1d95a0 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Nov 26, 2013 at 4:01 PM, Victor Hooi wrote: > Hi, > > I'm trying to use Python's new style string formatting with a dict and > string together. > > For example, I have the following dict and string variable: > > my_dict = { 'cat': 'ernie', 'dog': 'spot' } > foo = 'lorem ipsum' > > If I want to just use the dict, it all works fine: > > '{cat} and {dog}'.format(**my_dict) > 'ernie and spot' > > (I'm also curious how the above ** works in this case). > > However, if I try to combine them: > > '{cat} and {dog}, {}'.format(**my_dict, foo) > ... > SyntaxError: invalid syntax > Here you almost have it right. If you flip the arguments around to look like: '{cat} and {dog}, {}'.format(foo, **my_dict) it will work as you expect. The issue is that you cannot specify positional arguments (foo) after keyword arguments (**my_dict). In the code you tried, what Python is doing is: '{cat} and {dog}, {}'.format(cat=ernie, dog=spot, foo) which, if tried, provides the nicer error message of "SyntaxError: non-keyword arg after keyword arg". --089e0117691f36201304ec1d95a0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On T= ue, Nov 26, 2013 at 4:01 PM, Victor Hooi <victorhooi@gmail.com><= /span> wrote:
Hi,

I'm trying to use Python's new style string formatting with a dict = and string together.

For example, I have the following dict and string variable:

=A0 =A0 my_dict =3D { 'cat': 'ernie', 'dog': 's= pot' }
=A0 =A0 foo =3D 'lorem ipsum'

If I want to just use the dict, it all works fine:

=A0 =A0 '{cat} and {dog}'.format(**my_dict)
=A0 =A0 'ernie and spot'

(I'm also curious how the above ** works in this case).

However, if I try to combine them:

=A0 =A0 '{cat} and {dog}, {}'.format(**my_dict, foo)
=A0 =A0 ...
=A0 =A0 SyntaxError: invalid syntax

Her= e you almost have it right. If you flip the arguments around to look like:<= /div>
=A0 =A0 '{cat} and {dog}, {}'.format(foo, **my_dict)
<= /div>
it will work as you expect.

The issue is that= you cannot specify positional arguments (foo) after keyword arguments (**m= y_dict).

In the code you tried, what Python is doi= ng is:
=A0 =A0 '{cat} and {dog}, {}'.format(cat=3Dernie, dog=3Dspot, = foo)
which, if tried, provides the nicer error message of &qu= ot;SyntaxError: non-keyword arg after keyword arg".
<= /div> --089e0117691f36201304ec1d95a0--