Use of %s (Was: Python equivalent of "lynx -dump"?)

Ulf Engstrøm ulf.engstrom at b2b-link.com
Wed Mar 29 04:20:48 EST 2000


You're right if you're only talking about %s as the last part of a sentence,
but there's also some reasons to do it then.
Only talking for myself here, but I use %s almost all the time, and some of
my reasons are;
*First I have one param, then I want to add another one,
    'string with one %s' % (param,) ->
    'First %s and another %s' % (param1,param2)
    (Always be prepared ;)
*Creating an english plural by adding -s.
    animal = 'cat'
    'One %s' % animal -> 'Two %ss' % animal
*I often use sql-statements which requires "" over params, so you do:
'select * from table where name="%s"' % name which is more effiecient than
your proposal ('select * from table where name ="' + name + '"')

Well, anyways, just some thoughts :)
Regards
Ulf

> I hope you do not mind if I ask a Python style question unrelated to your
> issue.  That source line contains an idiom I see often in the Python code,
> and I do not understand why.  To me, it seems wasteful to use the %
> operator where simple string concatenation will do the same job more
> quickly.  For example, isn't this just as clear?
>
> lynxcmd = "lynx -dump " + url
>
> Certainly, in C programming, I would never use printf where a simple
strcat
> would do, and this seems like an exactly parallel situation.
>
> Now, admittedly, I am still quite new to Python.  I see that construct a
> LOT in the Python library and sample code, so if I'm missing some bigger
> picture here, I would appreciate a gentle correction.






More information about the Python-list mailing list