RE: Print function and spaces

Discussion in 'Python' started by Dan Williams, Feb 7, 2004.

  1. Dan Williams

    Dan Williams Guest

    Well, in the end I wrote this:


    def echo(*args):
    if (len(args) == 0):
    sys.stdout.write("\n")
    return

    for x in args:
    if (x != None):
    sys.stdout.write(str(x))

    if (x != None):
    sys.stdout.write("\n")


    I called it echo for obvious reasons !-)

    Basically it emulates print very closely, except it omits that annoying
    space.

    Maybe it'll be useful to someone other than myself :p

    Dan


    PS - I've used \n because that is what I am used to using. Should I be using
    \r\n...?



    -----Original Message-----
    From: python-list-bounces+dan=
    [mailto:python-list-bounces+dan=] On Behalf Of Diez B.
    Roggisch
    Sent: 05 February 2004 13:17
    To:
    Subject: Re: Print function and spaces

    > def PrintWithoutSpaces(*args):
    > output = ""
    > for i in args:
    > output = output + i
    >
    > print output
    >
    >
    > if __name__ == "__main__":
    > PrintWithoutSpaces("yo", "hello", "gutentag")
    > ---snip----
    >
    > this prints "yohellogutentag"


    You function won't work on mixed-type args:

    PrintWithoutSpaces("a", 10)
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<stdin>", line 4, in PrintWithoutSpaces
    TypeError: cannot concatenate 'str' and 'int' objects


    A better way would be this:

    def myprint(*args):
    print "".join([str(x) for x in args])


    --
    http://mail.python.org/mailman/listinfo/python-list
     
    Dan Williams, Feb 7, 2004
    #1
    1. Advertising

  2. I could have sworn there was a function to disable/enable placing spaces
    between objects printed using the print statement. I've looked all
    over, but I couldn't find it, so apparently it doesn't exist.

    - Josiah
     
    Josiah Carlson, Feb 7, 2004
    #2
    1. Advertising

  3. Josiah> I could have sworn there was a function to disable/enable
    Josiah> placing spaces between objects printed using the print
    Josiah> statement. I've looked all over, but I couldn't find it, so
    Josiah> apparently it doesn't exist.

    I think file().softspace is what you're looking for.

    Skip
     
    Skip Montanaro, Feb 7, 2004
    #3
  4. > Josiah> I could have sworn there was a function to disable/enable
    > Josiah> placing spaces between objects printed using the print
    > Josiah> statement. I've looked all over, but I couldn't find it, so
    > Josiah> apparently it doesn't exist.
    >
    > I think file().softspace is what you're looking for.


    That is it. Checking the docs, it doesn't seem to do what I thought it
    did, nor what the parent author had desired, and testing confirms this.

    Ah well.
    - Josiah
     
    Josiah Carlson, Feb 7, 2004
    #4
    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. Dan Williams

    Print function and spaces

    Dan Williams, Feb 5, 2004, in forum: Python
    Replies:
    9
    Views:
    335
    Paul Rubin
    Feb 5, 2004
  2. John B. Matthews
    Replies:
    4
    Views:
    684
    John B. Matthews
    Sep 12, 2008
  3. Roedy Green
    Replies:
    3
    Views:
    645
  4. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,654
    Jan C. Faerber
    Sep 10, 2009
  5. johkar
    Replies:
    2
    Views:
    2,996
    Mayeul
    Dec 10, 2009
Loading...

Share This Page