print with no newline

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

    print with no newline

    I thought that using a comma at the end of a print statement would suppress
    printing of a newline. Am I misunderstandin g this feature? How can I use
    print and not have a newline appended at the end?

    C:\src\projects \test1>python -c "import sys;print sys.version, 'running on',
    sys.platform"
    2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] running on
    win32

    C:\src\projects \test1>python -c "print 'here'," >jjj

    C:\src\projects \test1>od -c jjj
    0000000 h e r e \r \n
    0000006


    $ python -c "import sys;print sys.version, 'running on', sys.platform"
    2.1 (#1, May 23 2003, 11:43:56) [C] running on aix4

    $ cat eoltest.py
    #!/usr/bin/env python
    print 'here',

    $ ./eoltest.py >jjj

    $ od jjj
    0000000 068 065 072 065 00a
    h e r e \n
    0000005


  • Jp Calderone

    #2
    Re: print with no newline

    Paul Watson wrote:[color=blue]
    > I thought that using a comma at the end of a print statement would suppress
    > printing of a newline. Am I misunderstandin g this feature? How can I use
    > print and not have a newline appended at the end?
    >[/color]

    Print doesn't want to leave the *final* line without a newline.
    sys.stdout.writ e() doesn't care if your shell prompt gets mixed in with
    the last line of output. You'll need to use the latter if that's what
    you want.

    exarkun@boson:~ $ python -c "import sys; sys.stdout.writ e('here')"
    hereexarkun@bos on:~$

    Jp

    Comment

    • Larry Bates

      #3
      Re: print with no newline

      Print with no newline only works to stdout.
      If you want to write to a file without
      newlines use fp.write("text" ).

      import sys
      fp=open('jjj',' w')
      fp.write(sys.ve rsion)
      fp.write('runni ng on')
      fp.write(sys.pl atform)
      fp.close()

      after running jjj contains:

      2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)]running onwin32

      HTH,
      Larry Bates
      Syscon, Inc.

      "Paul Watson" <pwatson@redlin epy.com> wrote in message
      news:2prasrFo4r ruU1@uni-berlin.de...[color=blue]
      > I thought that using a comma at the end of a print statement would[/color]
      suppress[color=blue]
      > printing of a newline. Am I misunderstandin g this feature? How can I use
      > print and not have a newline appended at the end?
      >
      > C:\src\projects \test1>python -c "import sys;print sys.version, 'running[/color]
      on',[color=blue]
      > sys.platform"
      > 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] running on
      > win32
      >
      > C:\src\projects \test1>python -c "print 'here'," >jjj
      >
      > C:\src\projects \test1>od -c jjj
      > 0000000 h e r e \r \n
      > 0000006
      >
      >
      > $ python -c "import sys;print sys.version, 'running on', sys.platform"
      > 2.1 (#1, May 23 2003, 11:43:56) [C] running on aix4
      >
      > $ cat eoltest.py
      > #!/usr/bin/env python
      > print 'here',
      >
      > $ ./eoltest.py >jjj
      >
      > $ od jjj
      > 0000000 068 065 072 065 00a
      > h e r e \n
      > 0000005
      >
      >[/color]


      Comment

      • Peter Otten

        #4
        Re: print with no newline

        Paul Watson wrote:
        [color=blue]
        > I thought that using a comma at the end of a print statement would
        > suppress
        > printing of a newline. Am I misunderstandin g this feature? How can I use
        > print and not have a newline appended at the end?[/color]

        I thought that, too. It turns out that Python writes an additional newline
        on exit if the softspace flag is set. So

        $ python -c "import sys; print 'here',; sys.stdout.soft space = False" >
        tmp.txt
        $ od -c tmp.txt
        0000000 h e r e
        0000004

        is a viable if ugly workaround.

        Peter

        Comment

        • Paul Watson

          #5
          Re: print with no newline


          "Jp Calderone" <exarkun@divmod .com> wrote in message
          news:mailman.28 34.1094220495.5 135.python-list@python.org ...[color=blue]
          > Paul Watson wrote:[color=green]
          > > I thought that using a comma at the end of a print statement would[/color][/color]
          suppress[color=blue][color=green]
          > > printing of a newline. Am I misunderstandin g this feature? How can I[/color][/color]
          use[color=blue][color=green]
          > > print and not have a newline appended at the end?
          > >[/color]
          >
          > Print doesn't want to leave the *final* line without a newline.
          > sys.stdout.writ e() doesn't care if your shell prompt gets mixed in with
          > the last line of output. You'll need to use the latter if that's what
          > you want.
          >
          > exarkun@boson:~ $ python -c "import sys; sys.stdout.writ e('here')"
          > hereexarkun@bos on:~$
          >
          > Jp[/color]

          Ok, I can use sys.stdout.writ e(). Still, this comma at the end thing does
          not seem very consistent. Before the last line, while it does suppress the
          newline, a space is still added to the output. Why is that? Yes, I have
          seen spaces added between items in the print statement and, while it is
          probably convenient at times, is frequently an annoyance.

          C:\src\projects \test1>python -c "print 'here',;print 'there'," >jjj

          C:\src\projects \test1>od -c -tx1 jjj
          0000000 h e r e t h e r e \r \n
          68 65 72 65 20 74 68 65 72 65 0d 0a
          0000014


          Comment

          • Jp Calderone

            #6
            Re: print with no newline

            Paul Watson wrote:[color=blue]
            > "Jp Calderone" <exarkun@divmod .com> wrote in message
            > news:mailman.28 34.1094220495.5 135.python-list@python.org ...
            >[color=green]
            >>Paul Watson wrote:
            >>[color=darkred]
            >>>I thought that using a comma at the end of a print statement would[/color][/color]
            >
            > suppress
            >[color=green][color=darkred]
            >>>printing of a newline. Am I misunderstandin g this feature? How can I[/color][/color]
            >
            > use
            >[color=green][color=darkred]
            >>>print and not have a newline appended at the end?
            >>>[/color]
            >>
            >> Print doesn't want to leave the *final* line without a newline.
            >>sys.stdout.wr ite() doesn't care if your shell prompt gets mixed in with
            >>the last line of output. You'll need to use the latter if that's what
            >>you want.
            >>
            >>exarkun@boson :~$ python -c "import sys; sys.stdout.writ e('here')"
            >>hereexarkun@b oson:~$
            >>
            >> Jp[/color]
            >
            >
            > Ok, I can use sys.stdout.writ e(). Still, this comma at the end thing does
            > not seem very consistent. Before the last line, while it does suppress the
            > newline, a space is still added to the output. Why is that? Yes, I have
            > seen spaces added between items in the print statement and, while it is
            > probably convenient at times, is frequently an annoyance.[/color]

            Basically, print is only meant to help people new to the language get
            started ;) It often does what will make life easiest for someone who is
            just getting into things, but which is otherwise confusing, expected,
            special-casey, or otherwise undesirable. I mean, the whole existence of
            the keyword "print" is an inconsistency, right? One could quite
            reasonably expect print to be a function.

            Jp

            Comment

            • Benjamin Niemann

              #7
              Re: print with no newline

              You are not the only one, who feels that the behaviour of print is not
              optimal. Thta's why it is on the list of things to be dropped with
              Python 3. See PEP 3000 or "Python Regrets"
              (http://www.python.org/doc/essays/ppt...honRegrets.pdf)

              Paul Watson wrote:
              [color=blue]
              > "Jp Calderone" <exarkun@divmod .com> wrote in message
              > news:mailman.28 34.1094220495.5 135.python-list@python.org ...
              >[color=green]
              >>Paul Watson wrote:
              >>[color=darkred]
              >>>I thought that using a comma at the end of a print statement would[/color][/color]
              >
              > suppress
              >[color=green][color=darkred]
              >>>printing of a newline. Am I misunderstandin g this feature? How can I[/color][/color]
              >
              > use
              >[color=green][color=darkred]
              >>>print and not have a newline appended at the end?
              >>>[/color]
              >>
              >> Print doesn't want to leave the *final* line without a newline.
              >>sys.stdout.wr ite() doesn't care if your shell prompt gets mixed in with
              >>the last line of output. You'll need to use the latter if that's what
              >>you want.
              >>
              >>exarkun@boson :~$ python -c "import sys; sys.stdout.writ e('here')"
              >>hereexarkun@b oson:~$
              >>
              >> Jp[/color]
              >
              >
              > Ok, I can use sys.stdout.writ e(). Still, this comma at the end thing does
              > not seem very consistent. Before the last line, while it does suppress the
              > newline, a space is still added to the output. Why is that? Yes, I have
              > seen spaces added between items in the print statement and, while it is
              > probably convenient at times, is frequently an annoyance.
              >
              > C:\src\projects \test1>python -c "print 'here',;print 'there'," >jjj
              >
              > C:\src\projects \test1>od -c -tx1 jjj
              > 0000000 h e r e t h e r e \r \n
              > 68 65 72 65 20 74 68 65 72 65 0d 0a
              > 0000014
              >
              >[/color]

              Comment

              • Paul Watson

                #8
                Re: print with no newline

                "Peter Otten" <__peter__@web. de> wrote in message
                news:ch9vhg$n35 $01$1@news.t-online.com...[color=blue]
                > Paul Watson wrote:
                >[color=green]
                > > I thought that using a comma at the end of a print statement would
                > > suppress
                > > printing of a newline. Am I misunderstandin g this feature? How can I[/color][/color]
                use[color=blue][color=green]
                > > print and not have a newline appended at the end?[/color]
                >
                > I thought that, too. It turns out that Python writes an additional newline
                > on exit if the softspace flag is set. So
                >
                > $ python -c "import sys; print 'here',; sys.stdout.soft space = False" >
                > tmp.txt
                > $ od -c tmp.txt
                > 0000000 h e r e
                > 0000004
                >
                > is a viable if ugly workaround.
                >
                > Peter[/color]

                Many thanks for pointing out File.softspace attribute. However, I get mixed
                results when using it. I am sure there is some logic to it somewhere. It
                does not appear to control the end of line. The online doc says that it
                controls putting a space -before- another value. The File.softspace. __doc__
                string appears to need review also. I think I am ready to use File.write()
                and move on.

                C:\src\projects \test1>type eoltest.py
                #!/usr/bin/env python
                import sys
                print 'here', 'and'
                sys.stdout.soft space = False
                print 'here', 'and'
                sys.stdout.soft space = True
                print 'here', 'and'
                sys.stdout.soft space = False
                print 'there',

                C:\src\projects \test1>eoltest. py
                here and
                here and
                here and
                there

                C:\src\projects \test1>eoltest. py >jjj

                C:\src\projects \test1>od -c -tx1 jjj
                0000000 h e r e a n d \r \n h e r e a
                68 65 72 65 20 61 6e 64 0d 0a 68 65 72 65 20 61
                0000020 n d \r \n h e r e a n d \r \n t
                6e 64 0d 0a 20 68 65 72 65 20 61 6e 64 0d 0a 74
                0000040 h e r e \r \n
                68 65 72 65 0d 0a
                0000046

                C:\src\projects \test1>python -c "import sys;print
                sys.stdout.soft space.__doc__"
                int(x[, base]) -> integer

                Convert a string or number to an integer, if possible. A floating point
                argument will be truncated towards zero (this does not include a string
                representation of a floating point number!) When converting a string, use
                the optional base. It is an error to supply a base when converting a
                non-string. If the argument is outside the integer range a long object
                will be returned instead.


                Comment

                • Peter Otten

                  #9
                  Re: print with no newline

                  Paul Watson wrote:
                  [color=blue]
                  > Many thanks for pointing out File.softspace attribute. However, I get
                  > mixed
                  > results when using it. I am sure there is some logic to it somewhere. It
                  > does not appear to control the end of line. The online doc says that it
                  > controls putting a space -before- another value. The[/color]

                  The softspace is normally set when a string is printed but cleared when a
                  newline is encountered. The print statement uses it to determine whether a
                  space should precede the string it is about to write. By clearing it
                  manually you can omit the space between two strings:
                  [color=blue][color=green][color=darkred]
                  >>> import sys
                  >>> print "abc",;sys.stdo ut.softspace=0; print "def"[/color][/color][/color]
                  abcdef

                  When the program is terminated, the flag is (ab)used to determine whether a
                  line was started but not finished. Only then it controls whether a newline
                  is printed or not. My original idea was to register an exit handler that
                  does that, but it turned out that would be too late.
                  [color=blue]
                  > File.softspace. __doc__
                  > string appears to need review also.[/color]

                  The softspace attribute is just an ordinary integer, i. e. you get the same
                  docstring you get for any int instance - the docstring of the int class:
                  [color=blue][color=green][color=darkred]
                  >>> int.__doc__ == 42 .__doc__ # note the space after 42[/color][/color][/color]
                  True
                  [color=blue]
                  > I think I am ready to use File.write() and move on.[/color]

                  No objections here :-)

                  Peter

                  Comment

                  • Bengt Richter

                    #10
                    Re: print with no newline

                    On Fri, 03 Sep 2004 16:36:31 +0200, Peter Otten <__peter__@web. de> wrote:
                    [color=blue]
                    >Paul Watson wrote:
                    >[color=green]
                    >> I thought that using a comma at the end of a print statement would
                    >> suppress
                    >> printing of a newline. Am I misunderstandin g this feature? How can I use
                    >> print and not have a newline appended at the end?[/color]
                    >
                    >I thought that, too. It turns out that Python writes an additional newline
                    >on exit if the softspace flag is set. So
                    >
                    >$ python -c "import sys; print 'here',; sys.stdout.soft space = False" >
                    >tmp.txt
                    >$ od -c tmp.txt
                    >0000000 h e r e
                    >0000004
                    >
                    >is a viable if ugly workaround.
                    >[/color]
                    When I want printf-like control, I sometimes use (IIRC from last time ;-)
                    [color=blue][color=green][color=darkred]
                    >>> import sys
                    >>> def printf(fmt, *args): sys.stdout.writ e(fmt % args)[/color][/color][/color]
                    ...[color=blue][color=green][color=darkred]
                    >>> printf('here')[/color][/color][/color]
                    here>>>[color=blue][color=green][color=darkred]
                    >>> printf('here %s\n', 'doggie')[/color][/color][/color]
                    here doggie[color=blue][color=green][color=darkred]
                    >>>[/color][/color][/color]

                    Regards,
                    Bengt Richter

                    Comment

                    Working...