Discussion:
[Tutor] what %s=%s means?
韩宪平
2006-07-06 01:35:53 UTC
Permalink
I realy new to python.I try on learning it by code examples.The
following one from "Dive into python":

def buildConnectionString(params):
"""Build a connection string from a dictionary of parameters.

Returns string."""
return ";".join(["%s=%s" % (k, v) for k, v in params.items()])

if __name__ == "__main__":
myParams = {"server":"mpilgrim", \
"database":"master", \
"uid":"sa", \
"pwd":"secret" \
}
print buildConnectionString(myParams)

Whant ";"means
%s=%s?
join?
help(items) no matched.
Where can i find stuff helpful?
Ismael Garrido
2006-07-06 02:08:38 UTC
Permalink
Post by 韩宪平
I realy new to python.I try on learning it by code examples.The
"""Build a connection string from a dictionary of parameters.
Returns string."""
return ";".join(["%s=%s" % (k, v) for k, v in params.items()])
myParams = {"server":"mpilgrim", \
"database":"master", \
"uid":"sa", \
"pwd":"secret" \
}
print buildConnectionString(myParams)
Whant ";"means
%s=%s?
join?
help(items) no matched.
Where can i find stuff helpful?
"%s" % (param) is called string formatting. It builds a new string from
a format (the first part, that looks like a regular string with %s) and
the arguments. The idea is that the %s get replaced with the value of
the params.

String formatting allows many complex uses (for example, format decimal
numbers with 3 digits before the . and 2 after). If you want to learn
more about this you should look for the python docs or some tutorial
(Google is your Friend). Alan Gauld's tutorial is really good :)
http://www.freenetpages.co.uk/hp/alan.gauld/ (though it seems it doesn't
cover this particular topic).

";".method is a string method. Strings ("") are objects, and as such
they can be called (look for Object Oriented Programming if you don't
know what an object is). -Alan's tutorial does have this topic :) -. The
idea is basically that you ask a string to do something (in this case,
join the elements in the list with itself in the middle).


Ismael
w chun
2006-07-06 04:14:22 UTC
Permalink
Post by Ismael Garrido
Post by 韩宪平
Returns string."""
return ";".join(["%s=%s" % (k, v) for k, v in params.items()])
Whant ";"means
%s=%s?
join?
help(items) no matched.
Where can i find stuff helpful?
"%s" % (param) is called string formatting.
xian-ping,

what ismael says is correct... it means string formatting. for
example %s means string, %d means integer, etc. you use those format
strings, then give it real objects, for example, a string or an
integer, and it will format it all into one larger string.

for extra help in Chinese, you can refer to:

1. Chinese links from Python.org:
http://www.python.org/doc/nonenglish/#chinese

2. my book, "Core Python Programming" was translated into Chinese...
it is somewhat dated (until the new one comes out), but is relevant
for your question:

http://www.china-pub.com/computers/common/info.asp?id=3097

good luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyb
Andre Engels
2006-07-06 04:19:33 UTC
Permalink
Post by 韩宪平
I realy new to python.I try on learning it by code examples.The
"""Build a connection string from a dictionary of parameters.
Returns string."""
return ";".join(["%s=%s" % (k, v) for k, v in params.items()])
Whant ";"means
";" simply means the string consisting of the one character ;
Post by 韩宪平
%s=%s?
You'll have to look at the more complete expression here:
"%s=%s" % (k, v)

%s then means, in this string replace me by the next element in the
tuple following the %. Thus, the above will evaluate to the string
consisting of the string representation of k followed by the character
= followed by the string representation of v.

Some examples to make it clearer:
"I am %s"%("John")
will evaluate to:
"I am John"

"%s is a number"%(7)
will evaluate to:
"7 is a number", because the stuff in the tuple doesn't need to be a string

If x equals 1 and y equals 2, "%s+%s=%s"%(x,y,x+y)
will evaluate to:
"1+2=3"
Post by 韩宪平
join?
Join is a method of a string. Its syntax is:
x.join(y)
where x must be a string and y a sequence of strings. It denotes a
string, consisting of the strings in y, with x between each pair.
Again, a few examples will make it more clear:

" ".join(["I","am","a","boat"])
evaluates to:
"I am a boat"

"-abc-".join(["I","am","a","boat"])
evaluates to:
"I-abc-am-abc-a-abc-boat"

and
"".join(["I","am","a","boat"])
evaluates to:
"Iamaboat"
--
Andre Engels, ***@gmail.com
ICQ
Alan Gauld
2006-07-06 10:59:51 UTC
Permalink
I really new to python.I try on learning it by code examples.The
If you know another language then the official tutorial is the best
place to start, it explains all of the items on your question list.

Dive into Python is very good but it is really intended for
intermediate
users who want to "dive into" the deeper areas, its not ideal as
a starter tutorial. Once you go through the official tutor then "Dive"
should make a lot more sense.

OTOH IF you are new to programming in general then the official
tutor might still be too deep. There is a non programmers page
on the python web site with a list of tutorials for absolute
beginners.

Othrs seem to have answered most of your specific questions.

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

Loading...