Separator in print statement

Discussion in 'Python' started by Bertram Scharpf, Oct 14, 2003.

  1. Hi,

    when I write

    >>> print 'abc', 'def',
    >>> print 'ghi'


    I get the output 'abc def ghi\n'.

    Is there a way to manipulate the print
    statment that I get for example:

    'abc, def, ghi\n'

    I mean: can I substitute the ' ' separator produced from
    the comma operator by a e.g. ', ' or something else?

    Thanks in advance.

    Bertram

    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    Bertram Scharpf, Oct 14, 2003
    #1
    1. Advertising

  2. Bertram Scharpf

    Matt Goodall Guest

    Bertram Scharpf wrote:

    >Hi,
    >
    >when I write
    >
    > >>> print 'abc', 'def',
    > >>> print 'ghi'

    >
    >I get the output 'abc def ghi\n'.
    >
    >Is there a way to manipulate the print
    >statment that I get for example:
    >
    >'abc, def, ghi\n'
    >
    >I mean: can I substitute the ' ' separator produced from
    >the comma operator by a e.g. ', ' or something else?
    >


    print '%s, %s, %s' % ('abc', 'def', 'ghi')

    or better still

    print ', '.join(('abc', 'def', 'ghi'))

    If you want super-fine control then don't use print, use sys.stdout.write().

    Cheers, Matt

    --
    Matt Goodall, Pollenation Internet Ltd
    w: http://www.pollenationinternet.com
    e:
    Matt Goodall, Oct 14, 2003
    #2
    1. Advertising

  3. Bertram Scharpf

    Peter Hansen Guest

    Bertram Scharpf wrote:
    >
    > when I write
    >
    > >>> print 'abc', 'def',
    > >>> print 'ghi'

    >
    > I get the output 'abc def ghi\n'.
    >
    > Is there a way to manipulate the print
    > statment that I get for example:


    The general rule with "print" is that it works like it does, and
    if you don't like the way it works, you need to switch to something
    else.

    > 'abc, def, ghi\n'
    >
    > I mean: can I substitute the ' ' separator produced from
    > the comma operator by a e.g. ', ' or something else?


    If you require that the output be generated by separate statements
    or subroutine calls, then you will have to do something fairly
    complicated: create an object which acts like a file object, and
    which can collect blobs of data as you output them, but hold
    them in memory, writing them all out together after you send
    it the terminating sequence (\n in this case).

    A simpler option is just to collect up the bits of output that
    you need in a list, then use the string join() method to generate
    the output:

    outList = []
    outList.extend(['abc', 'def'])
    outList.append('ghi')
    print ', '.join(outList)

    I've included both the .extend() and .append() approaches, to
    most closely emulate your above example. You don't need to
    use .extend() if you don't want, and the code might be simpler
    if you don't.

    -Peter
    Peter Hansen, Oct 14, 2003
    #3
  4. Hi Peter,

    thank you for your detailed answer.

    Peter Hansen schrieb im Artikel <>:
    > The general rule with "print" is that it works like it does, and
    > if you don't like the way it works, you need to switch to something
    > else.


    Anyway. I mean ' ' when I say ' '.

    > If you require that the output be generated by separate statements
    > or subroutine calls, then you will have to do something fairly
    > complicated: create an object which acts like a file object, and
    > which can collect blobs of data as you output them, but hold
    > them in memory, writing them all out together after you send
    > it the terminating sequence (\n in this case).


    A nice idea; maybe I will have a closer look at it.

    > A simpler option is just to collect up the bits of output that
    > you need in a list, then use the string join() method to generate
    > the output:
    >
    > outList = []
    > outList.extend(['abc', 'def'])
    > outList.append('ghi')
    > print ', '.join(outList)
    >


    I think this list approach is what I really meant.

    Thanks, also to Matt,
    Bertram

    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    Bertram Scharpf, Oct 14, 2003
    #4
  5. Bertram Scharpf

    Peter Otten Guest

    Bertram Scharpf wrote:

    > when I write
    >
    > >>> print 'abc', 'def',
    > >>> print 'ghi'

    >
    > I get the output 'abc def ghi\n'.
    >
    > Is there a way to manipulate the print
    > statment that I get for example:
    >
    > 'abc, def, ghi\n'
    >
    > I mean: can I substitute the ' ' separator produced from
    > the comma operator by a e.g. ', ' or something else?


    Unfortunately, the delimiter for print is currently hardcoded.
    Here's some hackish code that will do what you want (most of the time).

    <code>
    import sys

    class DelimStream(object):
    def __init__(self, stream, delim):
    self.stream = stream
    self.delim = delim
    self._softspace = False
    def _set_softspace(self, b):
    if b:
    self._softspace = True
    def _get_softspace(self):
    return False
    softspace = property(_get_softspace, _set_softspace)
    def write(self, s):
    if self._softspace:
    if s != "\n":
    self.stream.write(self.delim)
    self._softspace = False
    self.stream.write(s)

    d = DelimStream(sys.stdout, ", ")

    #works most of the time
    print >> d, "foolish", "consistency", "hobgoblin", "little", "minds"
    print >> d, "seen", "hell", "himmel", "weich"

    #but not always:
    print "A", "B", "\n", "C"
    print >> d, "A", "B", "\n", "C" #note the missing delim before the C :)


    if 1: #not recommended
    sys.stdout = DelimStream(sys.stdout, ", ")
    print "foolish", "consistency", "hobgoblin", "little", "minds"
    </code>

    Peter
    Peter Otten, Oct 15, 2003
    #5
  6. On Tue, 14 Oct 2003 12:32:34 -0400, Peter Hansen <> wrote:

    >Bertram Scharpf wrote:
    >>
    >> when I write
    >>
    >> >>> print 'abc', 'def',
    >> >>> print 'ghi'

    >>
    >> I get the output 'abc def ghi\n'.
    >>
    >> Is there a way to manipulate the print
    >> statment that I get for example:

    >
    >The general rule with "print" is that it works like it does, and
    >if you don't like the way it works, you need to switch to something
    >else.


    Maybe something familiar? ;-)

    import sys
    def printf(fmt, *args): sys.stdout.write(fmt%args)

    or
    def sprintf(fmt, *args): return fmt%args # ok, a little different ;-)


    >
    >> 'abc, def, ghi\n'
    >>
    >> I mean: can I substitute the ' ' separator produced from
    >> the comma operator by a e.g. ', ' or something else?

    >
    >If you require that the output be generated by separate statements
    >or subroutine calls, then you will have to do something fairly
    >complicated: create an object which acts like a file object, and
    >which can collect blobs of data as you output them, but hold
    >them in memory, writing them all out together after you send
    >it the terminating sequence (\n in this case).
    >
    >A simpler option is just to collect up the bits of output that
    >you need in a list, then use the string join() method to generate
    >the output:
    >
    > outList = []
    > outList.extend(['abc', 'def'])
    > outList.append('ghi')
    > print ', '.join(outList)
    >
    >I've included both the .extend() and .append() approaches, to
    >most closely emulate your above example. You don't need to
    >use .extend() if you don't want, and the code might be simpler
    >if you don't.
    >
    >-Peter


    Regards,
    Bengt Richter
    Bengt Richter, Oct 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. keto
    Replies:
    0
    Views:
    881
  2. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    330
    David Cournapeau
    Dec 30, 2008
  3. ShaunJ
    Replies:
    12
    Views:
    328
    Peter J. Holzer
    Apr 17, 2008
  4. Peng Yu

    print array with separator?

    Peng Yu, Jan 25, 2010, in forum: Perl Misc
    Replies:
    31
    Views:
    307
    Dr.Ruud
    Jan 28, 2010
  5. Replies:
    0
    Views:
    246
Loading...

Share This Page