Simple thing about printing

Alex Martelli aleax at aleax.it
Thu Mar 13 03:48:01 EST 2003


Luke McCarthy wrote:

> It's funny how you use Python for a while and then something very simple
> comes up and stops you.
> How can I print without a newline being automatically inserted at the end?

If you end the statement with a comma you insert a space rather
than a newline, e.g.:

    print 'hello',
    print 'there'

emits the line

hello there


If you don't want the extra space either, don't use print, but rather
import sys and use sys.stdout.write, i.e.:

import sys
sys.stdout.write('hello')
sys.stdout.write('there\n')

emits the line

hellothere


Alex





More information about the Python-list mailing list