473,406 Members | 2,956 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,406 software developers and data experts.

No newline using printf

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
8 2484
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
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
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

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
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
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
Thanks for your help, Guys. This works of course.

-Samuel

Sep 16 '05 #8
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
4
by: Till Crueger | last post by:
Hi, I have a little problem with the following code: #include <stdio.h> int main(void) { char input='\0'; while(input!='q') { printf("Menu\n"); fflush(stdout);
4
by: bacadman | last post by:
I have a variable that seems to be placing a newline when outputting to a file. How can I test to see if this is the case.
29
by: runningdog | last post by:
Hi, I would like to be able to embed a newline in a text string. Is there any convienent notation to do this TIA Steve
4
by: weirdstuff | last post by:
Hi. I have this simple code: =========================================== ->Database query here (.. some code) $row=mysql_fetch_array($res); (...)
11
by: rossum | last post by:
I want to declare a const multi-line string inside a method, and I am having some problems using Environment.NewLine. I started out with: class foo { public void PrintStuff() { const...
11
by: Michael | last post by:
I'm new to PHP. I see that PHP supports the C printf function, and I've seen examples like printf("Hello world!\n"); however the newline character \n doesn't work - i.e., it does not generate an...
1
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
2
by: manontheedge | last post by:
I'm getting weird results in my program, and even compile time errors, and I've got it down to the "\n" command I use in "printf()". The program this is happening in deals with multiple processes,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
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
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...

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.