[Tutor] what %s=%s means?

Andre Engels andreengels at gmail.com
Thu Jul 6 06:19:33 CEST 2006


2006/7/6, 韩宪平 <hxianping at gmail.com>:
> 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()])

> Whant ";"means

";" simply means the string consisting of the one character ;

> %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"

> 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, andreengels at gmail.com
ICQ: 6260644  --  Skype: a_engels


More information about the Tutor mailing list