472,354 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

building strings from variables

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.

Oct 5 '06 #1
3 1759

Gal Diskin wrote:
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.
My opinion is that 1st method is the best. It's both very readable and
easy to write. 2nd way is too involved, and 3rd way is both hard to
read and difficult to compose. Another useful variant of method #1 is
to do this: %(varname)s ..." % globals()

Oct 5 '06 #2
Gal Diskin wrote:
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)
....
Or another for readability:

4. some_string = ' '.join(["cd", working_dir, ";",
ssh_cmd, str(some_count), some_param1, some_param2])

--Scott David Daniels
sc***********@acm.org
Oct 5 '06 #3
Matthew Warren wrote:
-----Original Message-----
From:
py*******************************************@pyth on.org
[mailto:py***************************************** **@python.o
rg] On Behalf Of Gal Diskin
Sent: 05 October 2006 16:01
To: py*********@python.org
Subject: building strings from variables

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.

:D

I think, it would depend really on what your aims are (readable code,
fast string generation...), and how the vars you want to build the
string from are respresented in your code (is it natural to use a dict
etc..)

I kicked off a conversation similar to this earlier today, and that was
my conclusion after helpful debate & advice.

Matt.
This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer.

You should not copy the email, use it for any purpose or disclose its contents to any other person.
Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
Matt,
Thanks for replying. I tried looking up your discussion and I'm unsure
if I found the right post. Would you mind linking to it?

--
Gal Diskin
G_********@Intel.com
G_********@gmail.com
Work: +972-4-865-1637
Cell: +972-54-7594166

Oct 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: meckhert | last post by:
I am currently building a sports web application and have run into a design question. In order to create a new coach record, there are two pieces of information that are needed: 1) username 2)...
26
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not properly pad with 0s. strQuantity prints as " ...
8
by: Olaf Meyer | last post by:
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...
2
by: ILCSP | last post by:
Hi, I have a sql table containing the answers for some tests. The information in this table is presented vertically and I need to create strings with them. I know how to read the data in VB.Net...
4
by: Hans | last post by:
Hi, I want to define a couple of constant strings, like in C: #define mystring "This is my string" or using a const char construction. Is this really not possible in Python? Hans
0
by: flameboy | last post by:
I am currently trying to figure my way how to output strings to a file. I have seen all the many posts and such how to do it in a console program, and have had much success doing it that way. This...
17
by: john | last post by:
All: I'm a long-time developer, new to PHP.... Is there an idiom used in PHP to construct SQL statments from $_POST data? I would guess that in many applications, the data read from $_POST...
3
by: patriciashoe | last post by:
Good AM: I have struggling with constructing a string to execute some SQL. Here is my update statement update teacherresources set = (( / variable1) * Variable 2 )) where(( trschool_id...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.