Printing style (was Re: [Tutor] Parsing a file)

Paul Sidorsky paulsid@shaw.ca
Sat, 16 Mar 2002 13:19:38 -0700


Erik Price wrote:

> > 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).

Yes, this looks like it comes directly from C, although I'm not familar
with PHP so I can't say for sure.

> 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?

Loosely, I'd say that's a good guideline, but it's probably a bit too
general.  

Sometimes it's easier or nicer to use the comma or string addition.  In
the case that precipitated this thread I think the comma is the way to
go.  It's clear, clean, and quick to type.  The comma method doesn't
apply to string formation outside of print, though.

If you are concatenating strings and don't need any spacing or
punctuation then I think addition is a little nicer.  Compare these two
examples:

print prefix + word + suffix
print "%s%s%s" % (prefix, word, suffix)

I prefer the first one in this case for its clean look, even though the
second one isn't that bad and could also be considered more explicit.

However, with punctuation, the % way is a lot less awkward I think:

print "To: " + boss + ", " + employee + ", " + secretary
print "To: %s, %s, %s" % (boss, employee, secretary)

When in doubt I tend to use % because if nothing else it provides the
flexibility to make changes (e.g. add punctuation, formatting) with less
effort.  Also I come from a C background where the % codes and printf(),
sprintf(), fprintf(), etc. are more or less the only legitimate way to
do this kind of thing.

Of course for matters of style I always look first to PEP 8, the Python
Style Guide, at:

http://python.sourceforge.net/peps/pep-0008.html

String formatting is not specifically covered in the style guide, but
the guide's preamble says just about everything one ever needs to know
about style in any language:

> A Foolish Consistency is the Hobgoblin of Little Minds
> 
>     A style guide is about consistency. Consistency with this style
>     guide is important.  Consistency within a project is more
>     important. Consistency within one module or function is most
>     important.
> 
>     But most importantly: know when to be inconsistent -- sometimes
>     the style guide just doesn't apply. When in doubt, use your best
>     judgement. Look at other examples and decide what looks best. And
>     don't hesitate to ask!

Whenever doubts about style arise, reread the above.  Frame it and hang
it from your wall if you need to!  While it does take a long time to
develop good judgement about style, you're a lot less likely to go too
far off track if you keep this advice at the front of your mind.

Out of curiousity I conducted an incomplete, unscientific, and utterly
biased survey of Python 2.1's standard library and got the following
impressions:

- The comma is not uncommon, especially for things like error messages:
print "Error:", errormsg

- Addition is used but not very often.  It seems to be used mostly for
short constructions that have no more than 3 parts.

- % is actually not as common as I thought in printing, but it's used an
awful lot for construction of strings elsewhere.

- There are also some other methods used, like the backquote notation. 
I've never used this so I don't know where it's useful.

Probably the only thing that can be concluded from this (if anything can
be) is that programmers are free to choose so long as they are
reasonably consistent about it, which generally seemed to be the case.

-- 
======================================================================
Paul Sidorsky                                          Calgary, Canada
paulsid@shaw.ca                        http://members.shaw.ca/paulsid/