473,554 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

print with no newline

I thought that using a comma at the end of a print statement would suppress
printing of a newline. Am I misunderstandin g this feature? How can I use
print and not have a newline appended at the end?

C:\src\projects \test1>python -c "import sys;print sys.version, 'running on',
sys.platform"
2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] running on
win32

C:\src\projects \test1>python -c "print 'here'," >jjj

C:\src\projects \test1>od -c jjj
0000000 h e r e \r \n
0000006
$ python -c "import sys;print sys.version, 'running on', sys.platform"
2.1 (#1, May 23 2003, 11:43:56) [C] running on aix4

$ cat eoltest.py
#!/usr/bin/env python
print 'here',

$ ./eoltest.py >jjj

$ od jjj
0000000 068 065 072 065 00a
h e r e \n
0000005
Jul 18 '05 #1
9 22156
Paul Watson wrote:
I thought that using a comma at the end of a print statement would suppress
printing of a newline. Am I misunderstandin g this feature? How can I use
print and not have a newline appended at the end?


Print doesn't want to leave the *final* line without a newline.
sys.stdout.writ e() doesn't care if your shell prompt gets mixed in with
the last line of output. You'll need to use the latter if that's what
you want.

exarkun@boson:~ $ python -c "import sys; sys.stdout.writ e('here')"
hereexarkun@bos on:~$

Jp
Jul 18 '05 #2
Print with no newline only works to stdout.
If you want to write to a file without
newlines use fp.write("text" ).

import sys
fp=open('jjj',' w')
fp.write(sys.ve rsion)
fp.write('runni ng on')
fp.write(sys.pl atform)
fp.close()

after running jjj contains:

2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)]running onwin32

HTH,
Larry Bates
Syscon, Inc.

"Paul Watson" <pw*****@redlin epy.com> wrote in message
news:2p******** ****@uni-berlin.de...
I thought that using a comma at the end of a print statement would suppress printing of a newline. Am I misunderstandin g this feature? How can I use
print and not have a newline appended at the end?

C:\src\projects \test1>python -c "import sys;print sys.version, 'running on', sys.platform"
2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] running on
win32

C:\src\projects \test1>python -c "print 'here'," >jjj

C:\src\projects \test1>od -c jjj
0000000 h e r e \r \n
0000006
$ python -c "import sys;print sys.version, 'running on', sys.platform"
2.1 (#1, May 23 2003, 11:43:56) [C] running on aix4

$ cat eoltest.py
#!/usr/bin/env python
print 'here',

$ ./eoltest.py >jjj

$ od jjj
0000000 068 065 072 065 00a
h e r e \n
0000005

Jul 18 '05 #3
Paul Watson wrote:
I thought that using a comma at the end of a print statement would
suppress
printing of a newline. Am I misunderstandin g 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.soft space = False" >
tmp.txt
$ od -c tmp.txt
0000000 h e r e
0000004

is a viable if ugly workaround.

Peter

Jul 18 '05 #4

"Jp Calderone" <ex*****@divmod .com> wrote in message
news:ma******** *************** *************** @python.org...
Paul Watson wrote:
I thought that using a comma at the end of a print statement would suppress printing of a newline. Am I misunderstandin g this feature? How can I use print and not have a newline appended at the end?


Print doesn't want to leave the *final* line without a newline.
sys.stdout.writ e() doesn't care if your shell prompt gets mixed in with
the last line of output. You'll need to use the latter if that's what
you want.

exarkun@boson:~ $ python -c "import sys; sys.stdout.writ e('here')"
hereexarkun@bos on:~$

Jp


Ok, I can use sys.stdout.writ e(). Still, this comma at the end thing does
not seem very consistent. Before the last line, while it does suppress the
newline, a space is still added to the output. Why is that? Yes, I have
seen spaces added between items in the print statement and, while it is
probably convenient at times, is frequently an annoyance.

C:\src\projects \test1>python -c "print 'here',;print 'there'," >jjj

C:\src\projects \test1>od -c -tx1 jjj
0000000 h e r e t h e r e \r \n
68 65 72 65 20 74 68 65 72 65 0d 0a
0000014
Jul 18 '05 #5
Paul Watson wrote:
"Jp Calderone" <ex*****@divmod .com> wrote in message
news:ma******** *************** *************** @python.org...
Paul Watson wrote:
I thought that using a comma at the end of a print statement would
suppress
printing of a newline. Am I misunderstandin g this feature? How can I
use
print and not have a newline appended at the end?


Print doesn't want to leave the *final* line without a newline.
sys.stdout.wr ite() doesn't care if your shell prompt gets mixed in with
the last line of output. You'll need to use the latter if that's what
you want.

exarkun@boson :~$ python -c "import sys; sys.stdout.writ e('here')"
hereexarkun@b oson:~$

Jp

Ok, I can use sys.stdout.writ e(). Still, this comma at the end thing does
not seem very consistent. Before the last line, while it does suppress the
newline, a space is still added to the output. Why is that? Yes, I have
seen spaces added between items in the print statement and, while it is
probably convenient at times, is frequently an annoyance.


Basically, print is only meant to help people new to the language get
started ;) It often does what will make life easiest for someone who is
just getting into things, but which is otherwise confusing, expected,
special-casey, or otherwise undesirable. I mean, the whole existence of
the keyword "print" is an inconsistency, right? One could quite
reasonably expect print to be a function.

Jp
Jul 18 '05 #6
You are not the only one, who feels that the behaviour of print is not
optimal. Thta's why it is on the list of things to be dropped with
Python 3. See PEP 3000 or "Python Regrets"
(http://www.python.org/doc/essays/ppt...honRegrets.pdf)

Paul Watson wrote:
"Jp Calderone" <ex*****@divmod .com> wrote in message
news:ma******** *************** *************** @python.org...
Paul Watson wrote:
I thought that using a comma at the end of a print statement would
suppress
printing of a newline. Am I misunderstandin g this feature? How can I
use
print and not have a newline appended at the end?


Print doesn't want to leave the *final* line without a newline.
sys.stdout.wr ite() doesn't care if your shell prompt gets mixed in with
the last line of output. You'll need to use the latter if that's what
you want.

exarkun@boson :~$ python -c "import sys; sys.stdout.writ e('here')"
hereexarkun@b oson:~$

Jp

Ok, I can use sys.stdout.writ e(). Still, this comma at the end thing does
not seem very consistent. Before the last line, while it does suppress the
newline, a space is still added to the output. Why is that? Yes, I have
seen spaces added between items in the print statement and, while it is
probably convenient at times, is frequently an annoyance.

C:\src\projects \test1>python -c "print 'here',;print 'there'," >jjj

C:\src\projects \test1>od -c -tx1 jjj
0000000 h e r e t h e r e \r \n
68 65 72 65 20 74 68 65 72 65 0d 0a
0000014

Jul 18 '05 #7
"Peter Otten" <__*******@web. de> wrote in message
news:ch******** *****@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 misunderstandin g 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.soft space = 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.soft space = False
print 'here', 'and'
sys.stdout.soft space = True
print 'here', 'and'
sys.stdout.soft space = 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.soft space.__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.
Jul 18 '05 #8
Paul Watson wrote:
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
The softspace is normally set when a string is printed but cleared when a
newline is encountered. The print statement uses it to determine whether a
space should precede the string it is about to write. By clearing it
manually you can omit the space between two strings:
import sys
print "abc",;sys.stdo ut.softspace=0; print "def" abcdef

When the program is terminated, the flag is (ab)used to determine whether a
line was started but not finished. Only then it controls whether a newline
is printed or not. My original idea was to register an exit handler that
does that, but it turned out that would be too late.
File.softspace. __doc__
string appears to need review also.
The softspace attribute is just an ordinary integer, i. e. you get the same
docstring you get for any int instance - the docstring of the int class:
int.__doc__ == 42 .__doc__ # note the space after 42

True
I think I am ready to use File.write() and move on.


No objections here :-)

Peter

Jul 18 '05 #9
On Fri, 03 Sep 2004 16:36:31 +0200, Peter Otten <__*******@web. de> wrote:
Paul Watson wrote:
I thought that using a comma at the end of a print statement would
suppress
printing of a newline. Am I misunderstandin g 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.soft space = False" >
tmp.txt
$ od -c tmp.txt
0000000 h e r e
0000004

is a viable if ugly workaround.

When I want printf-like control, I sometimes use (IIRC from last time ;-)
import sys
def printf(fmt, *args): sys.stdout.writ e(fmt % args) ... printf('here') here>>> printf('here %s\n', 'doggie') here doggie


Regards,
Bengt Richter
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
4135
by: Martin Bless | last post by:
Why can't we have an additional 'print' statement, that behaves exactly like 'print' but doesn't insert that damn blank between two arguments? Could be called 'printn' or 'prin1' or 'prinn' anything similar you like. Would make my life so much easier. Often I start with a nice and short print statement, then there happen to be more...
4
7202
by: fowlertrainer | last post by:
Hi ! I want to print, but without newline. I want to create a progress for ftp, but the print is drop a newline for every percent. I want like this: 0% 25% 50% 75% 100% But this happening: 0%
12
2385
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__ {}
14
2892
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version: $Revision: 0.0 $
2
5119
by: jamesthiele.usenet | last post by:
I recently ran into the issue with 'print' were, as it says on the web page called "Python Gotchas" (http://www.ferg.org/projects/python_gotchas.html): The Python Language Reference Manual says, about the print statement, A "\n" character is written at the end, unless the print statement ends with a comma. What it doesn't say is that...
0
1818
by: bearophileHUGS | last post by:
There is/was a long discussion about the replacement for print in Python 3.0 (I don't know if this discussion is finished): http://mail.python.org/pipermail/python-dev/2005-September/055968.html There is also a wiki page that collects the ideas: http://wiki.python.org/moin/PrintAsFunction There is the will to keep the printing operation...
11
2058
by: A.M | last post by:
Hi, I found print much more flexible that write method. Can I use print instead of file.write method? Thank you,
3
2103
by: James J. Besemer | last post by:
I would like to champion a proposed enhancement to Python. I describe the basic idea below, in order to gage community interest. Right now, it's only an idea, and I'm sure there's room for improvement. And of course it's possible there's some serious "gotcha" I've overlooked. Thus I welcome any and all comments. If there's some...
2
10997
by: Phoe6 | last post by:
print and softspace in python In python, whenever you use >>>print statement it will append a newline by default. If you don't want newline to be appended, you got use a comma at the end (>>>print 10,) When, you have a list of characters and want them to be printed together a string using a for loop, there was observation that no matter what...
0
7581
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8018
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7541
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7872
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6123
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5423
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3533
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2006
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
823
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.