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

Matt Nordhoff mnordhoff at mattnordhoff.com
Thu Apr 30 14:09:04 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.
> 
> 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

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.

Look:

>>> "%s" % ('foo',)
'foo'
>>> len("%s" % ('foo',))
3
>>> d = {}
>>> d["%s" % ('foo',)] = 1
>>> d
{'foo': 1}
>>> ("%s" % ('foo',)).upper()
'FOO'
>>>

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



More information about the Python-list mailing list