Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60558
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <torriem+gmail@torriefamily.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'subject:Python': 0.06; 'args': 0.07; 'subject:both': 0.07; 'suppose': 0.07; 'string': 0.09; 'arguments': 0.09; 'formatting': 0.09; 'parameter': 0.09; 'subject:()': 0.09; 'subject:string': 0.09; 'python': 0.11; "(i'm": 0.16; 'dict': 0.16; 'dictionary.': 0.16; 'dog': 0.16; 'example?': 0.16; 'format?': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'positional': 0.16; 'subject:Formatting': 0.16; 'subject:String': 0.16; 'syntaxerror:': 0.16; 'unpack': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'normally': 0.19; "python's": 0.19; 'work,': 0.20; 'command': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'case.': 0.24; 'skip:{ 20': 0.24; 'together.': 0.24; 'looks': 0.24; 'this:': 0.26; 'pass': 0.26; 'header:In- Reply-To:1': 0.27; 'tried': 0.27; "doesn't": 0.30; "i'm": 0.30; 'keys': 0.31; 'quotes': 0.31; 'another': 0.32; 'style': 0.33; "can't": 0.35; 'editor': 0.35; 'etc.)': 0.35; 'there': 0.35; 'curious': 0.36; 'keyword': 0.36; 'hi,': 0.36; 'example,': 0.37; 'two': 0.37; 'list': 0.37; 'message-id:@gmail.com': 0.38; 'work?': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'new': 0.61; 'simply': 0.61; "you'll": 0.62; 'needing': 0.65; 'within': 0.65; 'between': 0.67; 'invalid': 0.68; 'avoids': 0.84; 'dict,': 0.84; 'victor': 0.84 |
| X-Virus-Scanned | amavisd-new at torriefamily.org |
| Date | Tue, 26 Nov 2013 17:21:15 -0700 |
| From | Michael Torrie <torriem@gmail.com> |
| User-Agent | Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130105 Thunderbird/10.0.12 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Python String Formatting - passing both a dict and string to .format() |
| References | <fc656c78-a32b-4f85-8da6-a5c7abf1edfa@googlegroups.com> |
| In-Reply-To | <fc656c78-a32b-4f85-8da6-a5c7abf1edfa@googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | 7bit |
| 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.3265.1385511711.18130.python-list@python.org> (permalink) |
| Lines | 63 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1385511711 news.xs4all.nl 15881 [2001:888:2000:d::a6]:46013 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:60558 |
Show key headers only | View raw
On 11/26/2013 05: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
This is a syntax error because of the way that the ** unpacks the
dictionary. For this not to be a syntax error, foo has to be before
my_dict. This is because in parameter passing, keyword args are always
passed last. In general I don't think you want to unpack the dictionary
in this case.
> I also tried with:
>
> '{0['cat']} {1} {0['dog']}'.format(my_dict, foo) ... SyntaxError:
> invalid syntax
This is a syntax error because the cat and dog are not valid python
keywords. A string is anything between two delimiters. So your command
looks to python like this:
'{0[' cat ']} {1} {0[' dog ']}'.format(my_dict, foo)
If you have a proper syntax-highlighting editor you'll see right away
that cat and dog are not within the string delimiters.
This would work, however:
"{0['cat']} {1} {0['dog']}".format(my_dict, foo)
> However, I found that if I take out the single quotes around the keys
> it then works:
>
> '{0[cat]} {1} {0[dog]}'.format(my_dict, foo) "ernie lorem ipsum
> spot"
>
> I'm curious - why does this work? Why don't the dictionary keys need
> quotes around them, like when you normally access a dict's elements?
I suppose it's because the string formatter simply doesn't require it.
> Also, is this the best practice to pass both a dict and string to
> .format()? Or is there another way that avoids needing to use
> positional indices? ({0}, {1} etc.)
Can't you just list them as separate arguments to format? Like you did
in your working example?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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