building strings with variable input

Dave Benjamin ramen at lackingtalent.com
Sat Jan 17 22:09:19 CET 2004


In article <pduMb.6321$g4.137247 at news2.nokia.com>, Olaf Meyer wrote:
> Sometimes if find it clumsy unsing the following approach building strings:
> 
> cmd = "%s -start %s -end %s -dir %s" % (executable, startTime, endTime, 
> directory)
> 
> Especially if you have a lot of variable input it makes it hard to match 
> the variables to the proper fields. From other scripting languanges I'm 
> used to something like:
> 
>   $cmd = "$executable -start $startTime -end $endTime -dir $directory"
> 
> This makes it very easy to see how the string is actually built. You 
> dont't have to worry where which variables go.
> 
> Is there a similar way to do this in python?

Go here:
http://lfw.org/python/

Look under "string interpolation for Python".

Examples supported:

 "Here is a $string." 
 "Here is a $module.member." 
 "Here is an $object.member." 
 "Here is a $functioncall(with, arguments)." 
 "Here is an ${arbitrary + expression}." 
 "Here is an $array[3] member." 
 "Here is a $dictionary['member']."

Thanks to Ka-Ping Yee! I've succesfully used this to build a homebrew
templating language. It's nice and lightweight.

-- 
.:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g   l i f e   o u t   o f   t h e   c o n t a i n e r :



More information about the Python-list mailing list