Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60559

Re: Python String Formatting - passing both a dict and string to .format()

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 <ckaynor@zindagigames.com>
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&gt;': 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 <fc656c78-a32b-4f85-8da6-a5c7abf1edfa@googlegroups.com>
References <fc656c78-a32b-4f85-8da6-a5c7abf1edfa@googlegroups.com>
From Chris Kaynor <ckaynor@zindagigames.com>
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" <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3266.1385511717.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Tue, Nov 26, 2013 at 4:01 PM, Victor Hooi <victorhooi@gmail.com> 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".

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Python String Formatting - passing both a dict and string to .format() Victor Hooi <victorhooi@gmail.com> - 2013-11-26 16:01 -0800
  Re: Python String Formatting - passing both a dict and string to .format() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-27 00:14 +0000
  Re: Python String Formatting - passing both a dict and string to .format() Michael Torrie <torriem@gmail.com> - 2013-11-26 17:21 -0700
  Re: Python String Formatting - passing both a dict and string to .format() Chris Kaynor <ckaynor@zindagigames.com> - 2013-11-26 16:21 -0800

csiph-web