By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

No newline using printf

P: n/a
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

Sep 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Samuel wrote:
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.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
A life without festivity is a long road without an inn.
-- Democritus
Sep 16 '05 #2

P: n/a
In article <11*********************@z14g2000cwz.googlegroups. com>,
"Samuel" <kn******@gmail.com> wrote:
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))
Sep 16 '05 #3

P: n/a
On Thu, 2005-09-15 at 17:37 -0700, Samuel wrote:
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/)
gp****@aureal.com.pe
Tlf: (511) 243-0131
Nextel: 9824*4625

Sep 16 '05 #4

P: n/a

Roy Smith wrote:

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:
import sys
for i in range(3):

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

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

Sep 16 '05 #5

P: n/a
Johnny Lee wrote:
Roy Smith wrote:
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:
>>> import sys
>>> for i in range(3):

... 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
Sep 16 '05 #6

P: n/a
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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Sep 16 '05 #7

P: n/a
Thanks for your help, Guys. This works of course.

-Samuel

Sep 16 '05 #8

P: n/a
Samuel wrote:
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

Sep 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.