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"

    I know that I can do it in one go with

    But the prints happen in different places
    in the program.

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


    Tobiah, Sep 14, 2003
    1. Advertisements

  2. Tobiah

    Jarek Zgoda Guest

    Jarek Zgoda, Sep 14, 2003
    1. Advertisements

  3. 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.
    Yep, call sys.stdout.write directly.
    Erik Max Francis, Sep 14, 2003
  4. Hi!

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

    Tobias Pfeiffer, Sep 16, 2003
  5. Tobiah

    Duncan Booth Guest

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

    e.g..... j = pow(2,i)
    .... if j%100:
    .... sys.stdout.write("\b"+"/-\\|"[i%4])
    Maybe sys.stdout.flush()?
    Duncan Booth, Sep 16, 2003
  6. 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.
    You're encounting buffering; use sys.stdout.write directly and call
    sys.stdout.flush() after you've printed some partial output.
    Erik Max Francis, Sep 16, 2003
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.