print without newline?

Duncan Booth duncan at rcp.co.uk
Wed Aug 30 07:17:13 EDT 2000


noahspurrier at my-deja.com wrote in <8oi8ur$co4$1 at nnrp1.deja.com>:

>Is there a way to print without the newline (carriage return
>or line feed)? This does not do what I want:
>        print "Hello.",
>because it puts a space after the "Hello."

Actually, it doesn't put a space after the "Hello.", the next print 
statement puts a space before its output.

   print "Hello.",
   print "World"
prints "Hello. World" 
but:

   import sys
   print "Hello.",
   sys.stdout.softspace=0
   print "World"
prints "Hello.World"

>
>So I've been doing this:
>        sys.stdout.write ("Hello.")
>but this seems ugly, plus I have to import sys.
>
As shown above you can suppress the space, but you have to do it for every 
item you print, and you still have to import sys somewhere.
e.g.
    def nospace(s):
        import sys
        sys.stdout.softspace=0
        return s
    print "Hello.",nospace("World")
    print "Hello.",
    print nospace("World")



More information about the Python-list mailing list