Print always puts newline (or adds a space)

Discussion in 'Python' started by Tobiah, Sep 14, 2003.

  1. Tobiah

    Tobiah Guest

    This is a stumbling block for me. Is there
    a better way to output *just* what I want?

    I want the output "foobar"


    >>> print "foo"; print "bar"

    foo
    bar
    >>> print "foo",; print "bar"

    foo bar
    >>>


    I know that I can do it in one go with

    >>> print "%s%s" % ("foo", "bar")

    foobar

    But the prints happen in different places
    in the program.

    Is there a "lower level" way to output
    chars other than 'print'?

    Thanks,

    Tobiah
     
    Tobiah, Sep 14, 2003
    #1
    1. Advertising

  2. Tobiah

    Jarek Zgoda Guest

    Jarek Zgoda, Sep 14, 2003
    #2
    1. Advertising

  3. Tobiah wrote:

    > But the prints happen in different places
    > in the program.


    You can adjust this behavior with sys.stdout.softspace, but ultimately
    the print facility is not really ideal for when you need fine control
    over output.

    > Is there a "lower level" way to output
    > chars other than 'print'?


    Yep, call sys.stdout.write directly.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ It [freedom] must be demanded by the oppressed.
    \__/ Dr. Martin Luther King, Jr.
     
    Erik Max Francis, Sep 14, 2003
    #3
  4. Hi!

    Erik Max Francis <> wrote in news:3F64DEF7.F98CFAB8
    @alcyone.com:

    >> Is there a "lower level" way to output
    >> chars other than 'print'?

    >
    > Yep, call sys.stdout.write directly.


    Is it then also possible to "jump back" a few chars to, e.g., have a
    progress... (damn, I forgot that word) however, that shows the percentage
    of progress? Like print "25%" and then jump back three chars and write
    "26%"?
    And then, on my Linux machine, such things just don't happen at all. E.g.
    in a loop like...

    for i in range(1000):
    j = pow(2,i)
    if j%100: print "#",

    ....I will get nothing for a long time and then ten "#" chars at one time.
    Is there a solution for that?

    Bye
    Tobias
     
    Tobias Pfeiffer, Sep 16, 2003
    #4
  5. Tobiah

    Duncan Booth Guest

    Tobias Pfeiffer <> wrote in
    news:bk77de$pt7i4$-berlin.de:

    >> Yep, call sys.stdout.write directly.

    >
    > Is it then also possible to "jump back" a few chars to, e.g., have a
    > progress... (damn, I forgot that word) however, that shows the
    > percentage of progress? Like print "25%" and then jump back three
    > chars and write "26%"?


    Back three characters: "\b\b\b"
    Back to start of line: "\r"

    e.g.
    >>> for i in range(10000):

    .... j = pow(2,i)
    .... if j%100:
    .... sys.stdout.write("\b"+"/-\\|"[i%4])
    ....

    > And then, on my Linux machine, such things just don't happen at all.
    > E.g. in a loop like...
    >
    > for i in range(1000):
    > j = pow(2,i)
    > if j%100: print "#",
    >
    > ...I will get nothing for a long time and then ten "#" chars at one
    > time. Is there a solution for that?


    Maybe sys.stdout.flush()?


    --
    Duncan Booth
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
     
    Duncan Booth, Sep 16, 2003
    #5
  6. Tobias Pfeiffer wrote:

    > Is it then also possible to "jump back" a few chars to, e.g., have a
    > progress... (damn, I forgot that word) however, that shows the
    > percentage
    > of progress? Like print "25%" and then jump back three chars and write
    > "26%"?


    That's typically done with printing BS characters to back up one space
    ('\b') or CR characters to return the carriage to the beginning of the
    line ('\r'). Note that strictly speaking these may not have the desired
    effect, although in most environments they will.

    > And then, on my Linux machine, such things just don't happen at all.
    > E.g.
    > in a loop like...
    >
    > for i in range(1000):
    > j = pow(2,i)
    > if j%100: print "#",
    >
    > ...I will get nothing for a long time and then ten "#" chars at one
    > time.
    > Is there a solution for that?


    You're encounting buffering; use sys.stdout.write directly and call
    sys.stdout.flush() after you've printed some partial output.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ Custom reconciles us to everything.
    \__/ Edmund Burke
     
    Erik Max Francis, Sep 16, 2003
    #6
    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. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,986
    Bryan Bullard
    Jul 11, 2003
  2. Replies:
    4
    Views:
    293
    Ben Finney
    Jan 23, 2008
  3. Michael Brooks
    Replies:
    22
    Views:
    440
    Rick DeNatale
    Mar 27, 2007
  4. Raj Singh
    Replies:
    4
    Views:
    156
    Ryan Ingram
    Jan 29, 2008
  5. Replies:
    6
    Views:
    137
    7stud --
    Feb 9, 2009
Loading...

Share This Page