print without newline? 
Author Message
 print without newline?

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."

So I've been doing this:
        sys.stdout.write ("Hello.")
but this seems ugly, plus I have to import sys.

Yours,
Noah

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 16 Feb 2003 14:18:16 GMT  
 print without newline?

Quote:
> 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."

As you've noticed, the trailing comma does let you remove
the trailing newline, but only by substituting it with a space.

The spaces and newlines are considered the key differences
between the print statement, and the write method of file
objects, I think (well, there's also the distinction that
print forces its own stringizing, while the write method
needs you to use str() or whatever as appropriate).

Quote:
> So I've been doing this:
>         sys.stdout.write ("Hello.")
> but this seems ugly, plus I have to import sys.

It will seem less ugly if you avoid the space before the
open parenthesis, I think (I'm 100% with Guido in considering
this one of the worst possible uses of whitespace:-).

Alex



Sun, 16 Feb 2003 03:00:00 GMT  
 print without newline?
 >  So I've been doing this:
 >          sys.stdout.write ("Hello.")
 >  but this seems ugly, plus I have to import sys.

I guess it's the way to go. I wouldn't mind importing sys, I do that
all the time anyway. If you want to spare yourself some writing, you
can bind sys.stdout.write to a new name:

mywrite=sys.stdout.write

and then use mywrite.

/Mikael

-----------------------------------------------------------------------

WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael              
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date:    30-Aug-00
Time:    11:30:37

         /"\
         \ /     ASCII Ribbon Campaign
          X      Against HTML Mail
         / \

This message was sent by XF-Mail.
-----------------------------------------------------------------------



Sun, 16 Feb 2003 03:00:00 GMT  
 print without newline?

Quote:
>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"

Quote:

>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")


Sun, 16 Feb 2003 03:00:00 GMT  
 print without newline?


Quote:

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

I'm sure there is logic behind this choice, but it escapes me.

Can anyone explain why print should put a space out first?

And can they confirm that the FIRST print in the program does not.

Thanks

Ian

Ian Hobson

Every time we teach a child something, we prevent him from inventing
it himself. - Jean Piaget



Wed, 19 Feb 2003 08:09:35 GMT  
 print without newline?

Quote:

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

> I'm sure there is logic behind this choice, but it escapes me.

> Can anyone explain why print should put a space out first?

here's how it works:

a print statement is compiled into several bytecode instructions
(opcodes); there's one PRINT_ITEM for each comma-separated
item, and one PRINT_NEWLINE at the end (unless the statement
ends with a comma).

:::

in other words,

    print a, b, c

is compiled into:

    PRINT_ITEM a
    PRINT_ITEM b
    PRINT_ITEM c
    PRINT_NEWLINE

while

    print a, b, c,

results in:

    PRINT_ITEM a
    PRINT_ITEM b
    PRINT_ITEM c

each file-like object implements a "softspace" attribute, which
is used by these two opcodes.  if this flag is set, PRINT_ITEM
prints a space before the actual item.

PRINT_ITEM always sets the flag, PRINT_NEWLINE clears it:

Quote:
>>> print "a", ; sys.stdout.softspace = 0 ; print "b"
ab
>>> print "a" ; sys.stdout.softspace = 1 ; print "b"

a
 b

Quote:

> And can they confirm that the FIRST print in the program does not.

don't you think we would have noticed by now? ;-)

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->



Wed, 19 Feb 2003 21:21:21 GMT  
 print without newline?

    [snip]

Quote:
> >>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.

> I'm sure there is logic behind this choice, but it escapes me.
> Can anyone explain why print should put a space out first?

The print statement is (was) meant mainly as a convenience for
beginners & rapid insertion of "let's see what it's doing here" in
a program that's behaving strangely.  If it didn't use spaces by
default, then if you had
    print x,
and later
    print y,
and saw the output
1234
you'd have no way to know whether x was 1 and y 234, or x
was 12 and y was 34.  The space-by-default helps.

Quote:
> And can they confirm that the FIRST print in the program does not.

Just bind sys.output.softspace to 0 before any print, and you
can be sure it won't emit the initial space.  E.g. interactively:

Quote:
>>> print 12, ; print 34
12 34
>>> print 12, ; sys.stdout.softspace=0 ; print 34
1234

This of course suggests the usual kind of wrapping-object
trickery, e.g....:

class nospaces:
    def __init__(self,afile):
        self.afile=afile
    def __getattr__(self,attr):
        if attr=='softspace': return 0
        else: return getattr(self.afile,attr)
    def __setattr__(self,attr,value):
        if attr=='softspace': return
        else: self.__dict__[attr]=value

Quote:
>>> sys.stdout=Script1.nospaces(sys.stdout)
>>> print 12, ; print 34
1234

...but, resist the temptation: entertainment value apart,
using sys.stdout.write(str(foo)) (you can of course easily
wrap that in a function) is really simpler and more effective
than softspace trickery, IMHO.

Alex



Wed, 19 Feb 2003 21:52:04 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. print without newline

2. Printing without newlines..

3. Print characters on one line without newline...???

4. PEP 259: Omit printing newline after newline

5. PEP 259: Omit printing newline after newline

6. WRITE without newline?

7. write(*,*) without newline

8. Strip newline without losing implied space

9. write without newline

10. regsub of carriage return and newline without string map

11. How to print Subtotal Without Printing Detail

12. Newline Suppression in Print

 

 
Powered by phpBB® Forum Software