Print function and spaces

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

    Print function and spaces

    Hi people

    I'm getting a little annoyed with the way the print function always adds a
    space character between print statements unless there has been a new line.
    The manual mentions that "In some cases it may be functional to write an
    empty string to standard output for this reason." Am I the only the who
    thinks that this sucks? It's the first thing I've come across in Python that
    I really think is a design flaw.

    Is there a good way to stop the space being automatically generated, or am I
    going to have to write a blank string to standard output, like the manual
    mentions?

    Cheers

    Dan




  • Bjoern Paschen

    #2
    Re: Print function and spaces

    On Thu, 05 Feb 2004 11:38:26 +0000, Dan Williams wrote:
    [color=blue]
    > Is there a good way to stop the space being automatically generated, or am I
    > going to have to write a blank string to standard output, like the manual
    > mentions?[/color]
    I don't know if these are good ways, but i found this information about
    the topic on google:

    "The part about concatenation is important here"


    "How to turn off the automatic space completely"

    and i tried to implement the concatenation part into a small function
    (beware as i am new to python too ;)):

    ---snip----
    #/usr/bin/env python

    def PrintWithoutSpa ces(*args):
    output = ""
    for i in args:
    output = output + i

    print output


    if __name__ == "__main__":
    PrintWithoutSpa ces("yo", "hello", "gutentag")
    ---snip----

    this prints "yohellogutenta g"

    --
    _______________ _______________ _______________ _______________ _____
    Bjoern Paschen ._--_. Panasonic AVC Networks Germany GmbH
    paschen@mavd.de -- Audio Video Technology Centre


    Comment

    • Diez B. Roggisch

      #3
      Re: Print function and spaces

      > def PrintWithoutSpa ces(*args):[color=blue]
      > output = ""
      > for i in args:
      > output = output + i
      >
      > print output
      >
      >
      > if __name__ == "__main__":
      > PrintWithoutSpa ces("yo", "hello", "gutentag")
      > ---snip----
      >
      > this prints "yohellogutenta g"[/color]

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

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


      A better way would be this:

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


      Comment

      • Rich Krauter

        #4
        Re: Print function and spaces

        > Is there a good way to stop the space being automatically[color=blue]
        > generated, or am I
        > going to have to write a blank string to standard output, like the >[/color]
        manual mentions?

        You can try the write() method of file-like objects:

        import sys
        sys.stdout.writ e('%s test\n'%'This is a')

        print is a convenience, not necessarily a fine-grained formatting tool
        from what I understand.

        Rich

        On Thu, 2004-02-05 at 08:17, Diez B. Roggisch wrote:[color=blue][color=green]
        > > def PrintWithoutSpa ces(*args):
        > > output = ""
        > > for i in args:
        > > output = output + i
        > >
        > > print output
        > >
        > >
        > > if __name__ == "__main__":
        > > PrintWithoutSpa ces("yo", "hello", "gutentag")
        > > ---snip----
        > >
        > > this prints "yohellogutenta g"[/color]
        >
        > You function won't work on mixed-type args:
        >
        > PrintWithoutSpa ces("a", 10)
        > Traceback (most recent call last):
        > File "<stdin>", line 1, in ?
        > File "<stdin>", line 4, in PrintWithoutSpa ces
        > TypeError: cannot concatenate 'str' and 'int' objects
        >
        >
        > A better way would be this:
        >
        > def myprint(*args):
        > print "".join([str(x) for x in args])
        >[/color]

        Comment

        • wes weston

          #5
          Re: Print function and spaces



          Dan Williams wrote:[color=blue]
          > Hi people
          >
          > I'm getting a little annoyed with the way the print function always adds a
          > space character between print statements unless there has been a new line.
          > The manual mentions that "In some cases it may be functional to write an
          > empty string to standard output for this reason." Am I the only the who
          > thinks that this sucks? It's the first thing I've come across in Python that
          > I really think is a design flaw.
          >
          > Is there a good way to stop the space being automatically generated, or am I
          > going to have to write a blank string to standard output, like the manual
          > mentions?
          >
          > Cheers
          >
          > Dan
          >
          >
          >
          >[/color]
          Dan,
          'Does seem a little odd. There's often a good reason
          for python "oddities". Usually, it's a matter of practicality.
          Maybe it was thought that most intended uses of print are
          better of with a space.
          [color=blue][color=green][color=darkred]
          >>> a="a"
          >>> b="b"
          >>> print "%s%s" % (a,b)[/color][/color][/color]
          ab[color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

          Comment

          • Miki Tebeka

            #6
            Re: Print function and spaces

            Hello Dan,
            [color=blue]
            > Is there a good way to stop the space being automatically generated, or am I
            > going to have to write a blank string to standard output, like the manual
            > mentions?[/color]
            I use the % formatting and find it much better.

            HTH.
            Miki

            Comment

            • Bjoern Paschen

              #7
              Re: Print function and spaces

              On Thu, 05 Feb 2004 14:17:05 +0100, Diez B. Roggisch wrote:
              [color=blue]
              > You function won't work on mixed-type args:
              > A better way would be this:
              >
              > def myprint(*args):
              > print "".join([str(x) for x in args])[/color]
              Thanks. Works like a charm :)


              --
              _______________ _______________ _______________ _______________ _____
              Bjoern Paschen ._--_. Panasonic AVC Networks Germany GmbH
              paschen@mavd.de -- Audio Video Technology Centre


              Comment

              • Mel Wilson

                #8
                Re: Print function and spaces

                In article <bvtfop$vs5l8$1 @ID-111250.news.uni-berlin.de>,
                "Diez B. Roggisch" <nospam-deets@web.de> wrote:[color=blue][color=green]
                >> def PrintWithoutSpa ces(*args):
                >> output = ""
                >> for i in args:
                >> output = output + i
                >>
                >> print output[/color][/color]
                [ ... ][color=blue]
                >You function won't work on mixed-type args:
                >
                >PrintWithoutSp aces("a", 10)
                >Traceback (most recent call last):
                > File "<stdin>", line 1, in ?
                > File "<stdin>", line 4, in PrintWithoutSpa ces
                >TypeError: cannot concatenate 'str' and 'int' objects
                >
                >
                >A better way would be this:
                >
                >def myprint(*args):
                > print "".join([str(x) for x in args])[/color]

                True. Or just `output = output + str(i)` .
                The `str(i)` is the vital part.

                If the output string gets big, it will become plain that
                `"".join`... shown above is faster.

                Regards. Mel.

                Comment

                • Paul Miller

                  #9
                  using Numarray in a &quot;standalon e&quot; situation

                  Hey folks - got an interesting problem here.

                  I have an embedded Python interpreter and I'm packing the app,
                  python23.dll, and a subset of the Python 23 Lib directory for the utility
                  modules I need to use (so far, only os and random). I also need to use
                  numarray and I've copied the numarray directory into my standalone Lib
                  directory.

                  Within my code, I have set sys.path to point to my standalone Lib
                  directory, so in theory it shouldn't be looking in C:/Python23 for any
                  modules, but when I try to import numarray I see it still sees C:/Python23.

                  What other stuff do I need to do to make my integrated interpreter and
                  scripts I load to only see my standalone Lib directory as the Python library?

                  I hope this makes sense!



                  Comment

                  • Paul Rubin

                    #10
                    Re: Print function and spaces

                    "Dan Williams" <dan@ithium.net > writes:[color=blue]
                    > I'm getting a little annoyed with the way the print function always adds a
                    > space character between print statements unless there has been a new line.[/color]

                    Print is a statement, not a function.
                    [color=blue]
                    > The manual mentions that "In some cases it may be functional to write an
                    > empty string to standard output for this reason." Am I the only the who
                    > thinks that this sucks? It's the first thing I've come across in Python that
                    > I really think is a design flaw.[/color]

                    It's sort of a legacy thing, I believe. I don't like it either. It goes
                    against the Python principle that explicit is better than implicit. If
                    I want a space in the output, I'd rather ask for one.

                    Comment

                    Working...