print with no newline

Paul Watson pwatson at redlinepy.com
Fri Sep 3 11:10:14 EDT 2004


"Peter Otten" <__peter__ at web.de> wrote in message
news:ch9vhg$n35$01$1 at news.t-online.com...
> Paul Watson wrote:
>
> > I thought that using a comma at the end of a print statement would
> > suppress
> > printing of a newline.  Am I misunderstanding this feature?  How can I
use
> > print and not have a newline appended at the end?
>
> I thought that, too. It turns out that Python writes an additional newline
> on exit if the softspace flag is set. So
>
> $ python -c "import sys; print 'here',; sys.stdout.softspace = False" >
> tmp.txt
> $ od -c tmp.txt
> 0000000   h   e   r   e
> 0000004
>
> is a viable if ugly workaround.
>
> Peter

Many thanks for pointing out File.softspace attribute.  However, I get mixed
results when using it.  I am sure there is some logic to it somewhere.  It
does not appear to control the end of line.  The online doc says that it
controls putting a space -before- another value.  The File.softspace.__doc__
string appears to need review also.  I think I am ready to use File.write()
and move on.

C:\src\projects\test1>type eoltest.py
#!/usr/bin/env python
import sys
print 'here', 'and'
sys.stdout.softspace = False
print 'here', 'and'
sys.stdout.softspace = True
print 'here', 'and'
sys.stdout.softspace = False
print 'there',

C:\src\projects\test1>eoltest.py
here and
here and
 here and
there

C:\src\projects\test1>eoltest.py >jjj

C:\src\projects\test1>od -c -tx1 jjj
0000000   h   e   r   e       a   n   d  \r  \n   h   e   r   e       a
        68 65 72 65 20 61 6e 64 0d 0a 68 65 72 65 20 61
0000020   n   d  \r  \n       h   e   r   e       a   n   d  \r  \n   t
        6e 64 0d 0a 20 68 65 72 65 20 61 6e 64 0d 0a 74
0000040   h   e   r   e  \r  \n
        68 65 72 65 0d 0a
0000046

C:\src\projects\test1>python -c "import sys;print
sys.stdout.softspace.__doc__"
int(x[, base]) -> integer

Convert a string or number to an integer, if possible.  A floating point
argument will be truncated towards zero (this does not include a string
representation of a floating point number!)  When converting a string, use
the optional base.  It is an error to supply a base when converting a
non-string. If the argument is outside the integer range a long object
will be returned instead.





More information about the Python-list mailing list