A trivial question about print

Alex Martelli aleax at aleax.it
Thu Apr 11 11:44:54 EDT 2002


Ante Bagaric wrote:
        ...
> BTW, i never wrote a single line in Python but it looks darn attractive
> from what I have read so far. Just that this print behaviour brings some
> reminiscence to Fortran output formatting where some thing just happen and
> there's nothing you can do about it :)

print is not really meant for "production-level" output, such as
reporting and so on.  It's meant to be handy for quick & dirty
"let's see what this look like" things, and it is.

> Sure in most cases this feature helps, you dont need zillion " " strings
> between your variables :), but wouldnt it be nice if there was
> python-simple way to just get around it?

There IS -- it's sys.output.write (or more generally the write method
of any file object).  Does no formatting -- it emits a string, and
that's all.

The formatting you do by preparing the string.  The % operator is
most often what you want there.  You don't get full control since
it relies to some extent on the underlying C runtime library, for
example how many digits you get for the exponent in exponential
format is not directly controllable.  However, Python is very good
at string manipulation, so, once you've gotten "more or less" what
you want, typically with operator %, you can (in the relatively
rare cases where you need that) fine-tune things pretty easily.

Formatting a string is one job; putting strings out to some file,
another.  They happen together frequently enough to warrant some
shortcut for simple cases where nothing much is needed, which is
just about what the print statement gives you.  But for the fine
tuning needs, working on strings (for example via regular
expressions, but not necessarily) is a better idea than making
the print statement more complicated in its "joint job".


Alex




More information about the Python-list mailing list