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

Lie Ryan lie.1296 at
Thu Apr 30 17:37:45 CEST 2009

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.

There has never been print-with-formatting in python, what we have is 
the % string substitution operator, which is a string operation instead 
of print operation.

> I am wondering if there is a way to use print to write
> formatted output to files?

Of course:

f = open(...)
open.write('%s' % foo)


f = open(...)
print > f, '%s' % foo

or in python 3:
f = open(...)
print('%s' % foo, file=f)

or using the new superpowerful str.format()
 >>> 'foo: {0} bar: {baz:3.1f} {1:e}'.format(123, 456.789, baz = 123.456)
'foo: 123 bar: 123.5 4.567890e+02'

More information about the Python-list mailing list