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


Groups > comp.lang.python > #99184

Re: String format - resolve placeholders names

From Ervin Hegedüs <airween@gmail.com>
Newsgroups comp.lang.python
Subject Re: String format - resolve placeholders names
Date 2015-11-20 20:34 +0100
Message-ID <mailman.19.1448048061.2291.python-list@python.org> (permalink)
References <20151120145227.GB13994@arxnet.hu> <n2nfme$hfn$1@ger.gmane.org>

Show all headers | View raw


Hi Peter,

On Fri, Nov 20, 2015 at 04:53:47PM +0100, Peter Otten wrote:
> Ervin Hegedüs wrote:
> 
> > Python has a good string formatter, eg. I can do this:
> > 
> > s = "{who} likes {what}"
> > d = {'who': "Adam", 'what': "ants"}
> > s.format(**d)
> > 
> > result:
> > 'Adam likes ants'
> > 
> > Is it possible, and if yes, how to resolve the placeholders names
> > in string?
> 
> >>> import string
> >>> for item in string.Formatter().parse("{who} likes {what}"):
> ...     print(item)
> ... 
> ('', 'who', '', None)
> (' likes ', 'what', '', None)

nice solution, thanks,


a.
 

-- 
I � UTF-8

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


Thread

Re: String format - resolve placeholders names Ervin Hegedüs <airween@gmail.com> - 2015-11-20 20:34 +0100

csiph-web