473,666 Members | 2,144 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 14301
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.writ e().

Cheers, Matt

--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenationinternet.com
e: ma**@pollenatio n.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(obj ect):
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_s oftspace, _set_softspace)
def write(self, s):
if self._softspace :
if s != "\n":
self.stream.wri te(self.delim)
self._softspace = False
self.stream.wri te(s)

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

#works most of the time
print >> d, "foolish", "consistenc y", "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", "consistenc y", "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
74968
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 character? At one time, it was accepted as a truism that Windows (like MS-DOS) was different from Unix because Windows used the backslash as the path separator character, whereas Unix used the forward slash.
12
2394
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
2904
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: $Revision: 0.0 $
3
2455
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 documentation does not mention any way to do this. I know way back in 1998, Guido said he would consider adding it, but apparently that didn't happen. Is there some way to do this? -- "First they ignore you, then they laugh at you, then they fight...
69
3213
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 this pep would be to make it impossible to use the name 'print' in a backward compatible manner. Indeed, if a program is to compile in both Python 2.x and Python 3.x, the print function (or the print statement with parentheses) can not use the...
3
2170
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 of room somebody wants and 205 is the user ID in the Customer table.
1
1625
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 string containg the whole file) contains a so called 'paragraph separator' (unicode: 2029). If I want to pring that string (file) to the std out or a file object then I get a "UnicodeError" saying that the unicode 2029 can't be encoded...
7
10755
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 like it would be helpful with transitioning.
3
2561
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 know which each group is? Does this even make sense?
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
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 we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.