print "hello", >> file

Terry Reedy tjreedy at udel.edu
Tue Feb 25 17:32:51 CET 2003


"Gerhard Häring" <gerhard.haering at opus-gmbh.net> wrote in message
news:slrnb5memg.1i4.gerhard.haering at haering.opus-gmbh.net...
> Yar3k <programisci_listy at murator.org_zmien_na_com.pl> wrote:
> > Alex Martelli <aleax at aleax.it> wrote:
> >>    print >> f, "hello"
> >
> > why not f.write("hello") ?
>
> You mean f.write("hello\n")
>
> The difference is that print automatically adds a newline char.

It also automatically converts items to strings and joins multiple
items with spaces.

print >>f, 'hello', 1 #same as
f.write(' '.join(['hello', str(1)])+'\n') # or
f.write('hello'+' '+str(1)+'\n')

Terry J. Reedy






More information about the Python-list mailing list