Print always puts newline (or adds a space)

T

Tobiah

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

I want the output "foobar"


I know that I can do it in one go with
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
 
E

Erik Max Francis

Tobiah said:
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.
 
T

Tobias Pfeiffer

Hi!

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
 
D

Duncan Booth

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..... 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()?
 
E

Erik Max Francis

Tobias said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top