print(f) for files .. and is print % going away?

pruebauno at latinmail.com pruebauno at latinmail.com
Thu Apr 30 10:05:18 EDT 2009


On Apr 30, 8:30 am, Esmail <ebo... at hotmail.com> wrote:
> Matt Nordhoff wrote:
> > Esmail wrote:
> >> Hello all,
>
> >> I use the print method with % for formatting my output to
> >> the console since I am quite familiar with printf from my
> >> C days, and I like it quite well.
>
> >> I am wondering if there is a way to use print to write
> >> formatted output to files?
>
> >> Also, it seems like I read that formatting with print is
> >> going away eventually and we should start using something
> >> else? Is that true and if so, what?
>
> >> Thanks,
> >> Esmail
>
> Hi Matt,
>
> > String formatting has nothing to do with the print statement/function.
> > It's an operator, just like doing "foo" + "bar"; you can use it wherever
> > you want.
>
> Ah .. so this means I could use this formatting with the
> write method for files .. that is great news (you don't
> want to see the ugly code I put together there .. :-)
>
> > See <http://docs.python.org/library/stdtypes.html#string-formatting>
> > Also see <http://docs.python.org/library/string.html#formatstrings> for
> > information on the replacement for the old string formatting, Python
> > 2.6's new str.format().
>
> Will do .. so do you think it's good to move to the new format,
> or will the older one still be around for a while?
>
> Thanks again!
>
> Esmail

There is also the Template class in the stdlib string module, for unix
shell/perl style "formatting".

The old mod (%) formatting will be around in 3.1 and that version not
even out yet, so it will be around for a couple more years at the very
least. Although the new format is more powerful and slightly less
error prone, there is so much old code using % that there is
resistance to drop it. The printf format is also well known among C
programmers. One thing many Python programmers don't like is that it
is a single character operator (%). It makes formatting hard to spot.
Maybe printf formatting will be moved to some module in the stdlib in
the future as a compromise.

I hope that the information will help you decide what to use.



More information about the Python-list mailing list