No newline using printf

Discussion in 'Python' started by Samuel, Sep 16, 2005.

  1. Samuel

    Samuel Guest

    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
    Samuel, Sep 16, 2005
    #1
    1. Advertising

  2. 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 && && 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
    Erik Max Francis, Sep 16, 2005
    #2
    1. Advertising

  3. Samuel

    Roy Smith Guest

    In article <>,
    "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.


    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))
    Roy Smith, Sep 16, 2005
    #3
  4. 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/)

    Tlf: (511) 243-0131
    Nextel: 9824*4625
    Gustavo Picon, Sep 16, 2005
    #4
  5. Samuel

    Johnny Lee Guest

    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.
    Johnny Lee, Sep 16, 2005
    #5
  6. Samuel

    Peter Hansen Guest

    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
    Peter Hansen, Sep 16, 2005
    #6
  7. 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
    Sybren Stuvel, Sep 16, 2005
    #7
  8. Samuel

    Samuel Guest

    Thanks for your help, Guys. This works of course.

    -Samuel
    Samuel, Sep 16, 2005
    #8
  9. Samuel

    Gary Herron Guest

    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
    Gary Herron, Sep 16, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    4
    Views:
    600
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,223
  3. linq936

    Confused at newline requirement for printf

    linq936, Sep 21, 2007, in forum: C Programming
    Replies:
    1
    Views:
    297
    Eric Sosman
    Sep 21, 2007
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    423
  5. guru
    Replies:
    8
    Views:
    264
Loading...

Share This Page