building strings from variables

Gal Diskin gal.diskin at gmail.com
Thu Oct 5 17:00:58 CEST 2006


Following a discussion with an associate at work about various ways to
build strings from variables in python, I'd like to hear your opinions
and preferred methods. The methods we discussed are:
1.  some_string = "cd %s ; %s  %d %s %s" % ( working_dir, ssh_cmd,
some_count, some_param1, some_param2)

2. import string
    template = string.Template("cd $dir ; $cmd $count $param1
$param2")
    some_string = template.substitute(dict(dir=working_dir,

cmd=ssh_cmd,

count=some_count,

pararm1=some_param1,

param2=some_param2))
here you can use a couple of nice tricks by using class.__dict__ and
globals() \ locals() dictionaries.

3. some_string = "cd "+working_dir+" ; "+ssh_cmd+ "
"+str(some_count)+" "+some_param1+" "+some_param2
(all these are supposed to produce the same strings)

Which methods do you know of \ prefer \ think is better because...?
I will appreciate any opinions about the matter.




More information about the Python-list mailing list