[Tutor] Parsing a file
Erik Price
erikprice@mac.com
Sat, 16 Mar 2002 13:12:39 -0500
On Friday, March 15, 2002, at 04:35 PM, Paul Sidorsky wrote:
> Lastly, the most common way to print multiple pieces of data is like
> this:
>
> print "%s %s" % (badword, line)
>
> If you stick some formatting stuff in there you can even get them to
> line up
> nicely:
>
> print "%-10s %-60s" % (badword, line)
>
> The % syntax is probably overkill for this, though, and since it's not
> all that easy to use unless you already know C (which is where it comes
> from) you can probably live without it for now.
I see this all the time -- in PHP it's printf() (to echo a formatted
string) or sprintf() (to format a string and place it into a variable).
I have only used it once, though. It seems that in a lot of tutorials,
it is used frequently, and now I hear it confirmed by a Python-tutor
sage that it is "the most common way to print multiple pieces of data".
That interests me -- so, as a good rule of thumb, use "print 'x'" for
printing single pieces of data but print '%s %s' (x, y) when printing
multiple pieces?
Thank you for this style advice, of which I am always wondering.
Erik