473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Print always puts newline (or adds a space)

This is a stumbling block for me. Is there
a better way to output *just* what I want?

I want the output "foobar"

print "foo"; print "bar" foo
bar print "foo",; print "bar" foo bar
I know that I can do it in one go with
print "%s%s" % ("foo", "bar")

foobar

But the prints happen in different places
in the program.

Is there a "lower level" way to output
chars other than 'print'?

Thanks,

Tobiah

Jul 18 '05 #1
5 7781
Tobiah <to**@rcsreg.com> pisze:
Is there a "lower level" way to output
chars other than 'print'?


sys.stdout.write()
--
Jarek Zgoda
Registered Linux User #-1
http://www.zgoda.biz/ JID:ja***@jabberpl.org http://zgoda.jogger.pl/
Jul 18 '05 #2
Tobiah wrote:
But the prints happen in different places
in the program.
You can adjust this behavior with sys.stdout.softspace, but ultimately
the print facility is not really ideal for when you need fine control
over output.
Is there a "lower level" way to output
chars other than 'print'?


Yep, call sys.stdout.write directly.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ It [freedom] must be demanded by the oppressed.
\__/ Dr. Martin Luther King, Jr.
Jul 18 '05 #3
Hi!

Erik Max Francis <ma*@alcyone.com> wrote in news:3F64DEF7.F98CFAB8
@alcyone.com:
Is there a "lower level" way to output
chars other than 'print'?


Yep, call sys.stdout.write directly.


Is it then also possible to "jump back" a few chars to, e.g., have a
progress... (damn, I forgot that word) however, that shows the percentage
of progress? Like print "25%" and then jump back three chars and write
"26%"?
And then, on my Linux machine, such things just don't happen at all. E.g.
in a loop like...

for i in range(1000):
j = pow(2,i)
if j%100: print "#",

....I will get nothing for a long time and then ten "#" chars at one time.
Is there a solution for that?

Bye
Tobias
Jul 18 '05 #4
Tobias Pfeiffer <Bo**************@web.de> wrote in
news:bk************@ID-162581.news.uni-berlin.de:
Yep, call sys.stdout.write directly.
Is it then also possible to "jump back" a few chars to, e.g., have a
progress... (damn, I forgot that word) however, that shows the
percentage of progress? Like print "25%" and then jump back three
chars and write "26%"?


Back three characters: "\b\b\b"
Back to start of line: "\r"

e.g.
for i in range(10000):

.... j = pow(2,i)
.... if j%100:
.... sys.stdout.write("\b"+"/-\\|"[i%4])
....
And then, on my Linux machine, such things just don't happen at all.
E.g. in a loop like...

for i in range(1000):
j = pow(2,i)
if j%100: print "#",

...I will get nothing for a long time and then ten "#" chars at one
time. Is there a solution for that?


Maybe sys.stdout.flush()?
--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #5
Tobias Pfeiffer wrote:
Is it then also possible to "jump back" a few chars to, e.g., have a
progress... (damn, I forgot that word) however, that shows the
percentage
of progress? Like print "25%" and then jump back three chars and write
"26%"?
That's typically done with printing BS characters to back up one space
('\b') or CR characters to return the carriage to the beginning of the
line ('\r'). Note that strictly speaking these may not have the desired
effect, although in most environments they will.
And then, on my Linux machine, such things just don't happen at all.
E.g.
in a loop like...

for i in range(1000):
j = pow(2,i)
if j%100: print "#",

...I will get nothing for a long time and then ten "#" chars at one
time.
Is there a solution for that?


You're encounting buffering; use sys.stdout.write directly and call
sys.stdout.flush() after you've printed some partial output.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Custom reconciles us to everything.
\__/ Edmund Burke
Jul 18 '05 #6

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

Similar topics

4
by: Nancy Drew | last post by:
this script used to upload files to the server without any issue: $oFTPConn = ftp_connect($sFtpServer,21,600) or die("Unable to connect ot $sFTPServer!"); //login to the ftp server and upload the...
30
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'...
4
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:...
9
by: Paul Watson | last post by:
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...
12
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
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:...
0
by: Howard Kaikow | last post by:
I have never seen WriteLine fail to produce a NewLine, at least, not until now: I was playing with an example given on pages 51-52 of the book "A Visual Basic 6 Programmer's Toolkit". The book...
2
by: sixtyfootersdude | last post by:
Good Morning! I am just starting to learn perl and I am somewhat mistifide about when I should do: print("@input"); and when I should do: print(@input)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...

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.