String format - resolve placeholders names

Ervin Hegedüs airween at gmail.com
Fri Nov 20 14:34:10 EST 2015


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



More information about the Python-list mailing list