No newline using printf

S

Samuel

Hello,

I have been searching for an answer for almost two hours now and have
not found an answer. I want this code:

for i in range(3):
print i # or whatever

To produce this output:
012

How can I print a word without appending a newline character? Appending
a "," to the print statement only substitutes the newline for a space,
which is not what I am looking for.

Any hints?

Thanks,
-Samuel
 
E

Erik Max Francis

Samuel said:
How can I print a word without appending a newline character? Appending
a "," to the print statement only substitutes the newline for a space,
which is not what I am looking for.

Use sys.stdout.write directly.
 
R

Roy Smith

"Samuel said:
How can I print a word without appending a newline character? Appending
a "," to the print statement only substitutes the newline for a space,
which is not what I am looking for.

For closer control over output, use the write() function. You want
something like:

import sys
for i in range(3):
sys.stdout.write (str(i))
 
G

Gustavo Picon

Hello,

I have been searching for an answer for almost two hours now and have
not found an answer. I want this code:

for i in range(3):
print i # or whatever

To produce this output:
012

How can I print a word without appending a newline character? Appending
a "," to the print statement only substitutes the newline for a space,
which is not what I am looking for.

Try with:

print ''.join(str(foo) for foo in range(3))


or sys.stdout.write


--
Gustavo Picon (http://tabo.aurealsys.com/)
Aureal Systems S.A.C. (http://www.aureal.com.pe/)
(e-mail address removed)
Tlf: (511) 243-0131
Nextel: 9824*4625
 
J

Johnny Lee

Roy said:
For closer control over output, use the write() function. You want
something like:

import sys
for i in range(3):
sys.stdout.write (str(i))

here is the output of my machine:
... sys.stdout.write(str(i))
...
012>>>

Why the prompt followed after the output? Maybe it's not as expected.
 
P

Peter Hansen

Johnny said:
here is the output of my machine:

... sys.stdout.write(str(i))
...
012>>>

Why the prompt followed after the output? Maybe it's not as expected.

Because, unlike print, sys.stdout.write() just sends the raw bytes
directly to the output without special formatting, extra characters
(such as the newline print adds for you), or other interference.

Add the newline yourself after the loop to fix this:

sys.stdout.write('\n')

-Peter
 
S

Sybren Stuvel

Johnny Lee enlightened us with:
Why the prompt followed after the output? Maybe it's not as
expected.

Because it did what you ask of it: write "012" to stdout, and nothing
else. Hence, no newline at the end, hence the prompt is on the same
line.

Sybren
 
G

Gary Herron

Samuel said:
Hello,

I have been searching for an answer for almost two hours now and have
not found an answer. I want this code:

for i in range(3):
print i # or whatever

To produce this output:
012

How can I print a word without appending a newline character? Appending
a "," to the print statement only substitutes the newline for a space,
which is not what I am looking for.

Any hints?

Thanks,
-Samuel
The solution is to take over full control of the output with
sys.stdout.write.

Use '%1d' % i to convert your number into a single character string.

Use sys.stdout.write to send exactly the characters you want to sys.stdout.

Thus: sys.stdout.write('%1d' % i) should do what you want.

Dr Gary Herron
Digipen Institute of Technology
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top