473,507 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Separator in print statement


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
Jul 18 '05 #1
4 14282
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
Jul 18 '05 #2
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: ma**@pollenation.net

Jul 18 '05 #3
Hi Peter,

thank you for your detailed answer.

Peter Hansen schrieb im Artikel <3F***************@engcorp.com>:
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
Jul 18 '05 #4
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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
74827
by: Stephen Ferg | last post by:
I have a question that is not directly Python-related. But I thought I'd ask the most erudite group that I know... :-) When did Windows start accepting the forward slash as a path separator...
12
2378
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
14
2882
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
3
2436
by: Angelic Devil | last post by:
I know this has been asked before (I already consulted the Google Groups archive), but I have not seen a definative answer. Is there a way to change the record separator in readlines()? The...
69
3200
by: Edward K Ream | last post by:
The pros and cons of making 'print' a function in Python 3.x are well discussed at: http://mail.python.org/pipermail/python-dev/2005-September/056154.html Alas, it appears that the effect of...
3
2160
by: Simon Gare | last post by:
Hi All, I have a querystring that contains the + sign as a separator, I need to read these values individually in a select statement, for example. &text=Single+205 where single is the type...
1
1616
by: Dominique.Holzwarth | last post by:
Hello everyone I'm doing a transformation with (MSXML) from a xml file to a tex file (actually it's just output format "text" with tex commands inside). The output of the transformation (a big...
7
10749
by: samslists | last post by:
Am I the only one that thinks this would be useful? :) I'd really like to be able to use python 3.0's print statement in 2.x. Is this at least being considered as an option for 2.6? It seems...
3
2551
by: artemetis | last post by:
Hello my friends! It's been a while. I've created a report that has some 1200 records. It's grouped on department. Is it possible to have my report print out and have a separator page so I...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.