No newline using printf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Samuel

    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

  • Erik Max Francis

    #2
    Re: No newline using printf

    Samuel wrote:
    [color=blue]
    > 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.[/color]

    Use sys.stdout.writ e directly.

    --
    Erik Max Francis && max@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

    Comment

    • Roy Smith

      #3
      Re: No newline using printf

      In article <1126831050.903 747.13040@z14g2 000cwz.googlegr oups.com>,
      "Samuel" <knipknap@gmail .com> wrote:
      [color=blue]
      > 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.[/color]

      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))

      Comment

      • Gustavo Picon

        #4
        Re: No newline using printf

        On Thu, 2005-09-15 at 17:37 -0700, Samuel wrote:[color=blue]
        > 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.
        >[/color]

        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/)
        gpicon@aureal.c om.pe
        Tlf: (511) 243-0131
        Nextel: 9824*4625

        Comment

        • Johnny Lee

          #5
          Re: No newline using printf


          Roy Smith wrote:[color=blue]
          >
          > 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))[/color]

          here is the output of my machine:
          [color=blue][color=green][color=darkred]
          >>> import sys
          >>> for i in range(3):[/color][/color][/color]
          ... sys.stdout.writ e(str(i))
          ...
          012>>>

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

          Comment

          • Peter Hansen

            #6
            Re: No newline using printf

            Johnny Lee wrote:[color=blue]
            > Roy Smith wrote:
            >[color=green]
            >>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))[/color]
            >
            >
            > here is the output of my machine:
            >[color=green][color=darkred]
            > >>> import sys
            > >>> for i in range(3):[/color][/color]
            > ... sys.stdout.writ e(str(i))
            > ...
            > 012>>>
            >
            > Why the prompt followed after the output? Maybe it's not as expected.[/color]

            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

            Comment

            • Sybren Stuvel

              #7
              Re: No newline using printf

              Johnny Lee enlightened us with:[color=blue]
              > Why the prompt followed after the output? Maybe it's not as
              > expected.[/color]

              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

              Comment

              • Samuel

                #8
                Re: No newline using printf

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

                -Samuel

                Comment

                • Gary Herron

                  #9
                  Re: No newline using printf

                  Samuel wrote:
                  [color=blue]
                  >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
                  >
                  >
                  >[/color]
                  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

                  Comment

                  Working...