String formatting with the format string syntax

Peter Otten __peter__ at web.de
Wed Sep 15 02:38:40 EDT 2010


Andre Alexander Bell wrote:

> On 09/14/2010 08:20 PM, Miki wrote:
>> You can use ** syntax:
>>>>> english = {'hello':'hello'}
>>>>> s.format(**english)
> 
> Thanks for your answer. Actually your answer tells me that my example
> was misleading. Consider the template
> 
> s = 'A template with {variable1} and {variable2} placeholders.'
> 
> I'm seeking a way to extract the named placesholders, i.e. the names
> 'variable1' and 'variable2' from the template. I'm not trying to put in
> values for them.
> 
> I hope this is clearer.

>>> s = 'A template with {variable1} and {variable2} placeholders.'
>>> [name for _, name, _, _ in s._formatter_parser() if name is not None]
['variable1', 'variable2']

Peter



More information about the Python-list mailing list