473,587 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2490
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.writ e 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************ *********@z14g2 000cwz.googlegr oups.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.writ e (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.writ e
--
Gustavo Picon (http://tabo.aurealsys.com/)
Aureal Systems S.A.C. (http://www.aureal.com.pe/)
gp****@aureal.c om.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.writ e (str(i))


here is the output of my machine:
import sys
for i in range(3):

... sys.stdout.writ e(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.writ e (str(i))

here is the output of my machine:
>>> import sys
>>> for i in range(3):

... sys.stdout.writ e(str(i))
...
012>>>

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


Because, unlike print, sys.stdout.writ e() 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.writ e('\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.writ e.

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

Use sys.stdout.writ e to send exactly the characters you want to sys.stdout.

Thus: sys.stdout.writ e('%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
22163
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 end? C:\src\projects\test1>python -c "import sys;print sys.version, 'running on', sys.platform" 2.3.4 (#53, May 25 2004, 21:17:02) running on win32 C:\src\projects\test1>python -c "print 'here'," >jjj
4
9489
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
2409
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
3433
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
3418
by: weirdstuff | last post by:
Hi. I have this simple code: =========================================== ->Database query here (.. some code) $row=mysql_fetch_array($res); (...)
11
6223
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 string multiline = "The first line." + Environment.NewLine +
11
24878
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 HTML <br>, which I would have expected - what it does is generate a newline in the html generated text, but since the browser ignores blank lines, this feature appears useless for most applications.
1
8495
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
2457
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, so I'm forking and killing processes during the program. The code runs perfectly fine, UNTIL I try to use printf() with ANYTHING containing "\n" ... doesn't matter how many of them there are in the printf() statement, it always either gives a...
0
7923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7852
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7974
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.