By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

Print function and spaces

P: n/a
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


Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Thu, 05 Feb 2004 11:38:26 +0000, Dan Williams wrote:
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?

I don't know if these are good ways, but i found this information about
the topic on google:
http://www-106.ibm.com/developerwork...html?dwzone=ws
"The part about concatenation is important here"

http://www.faqts.com/knowledge_base/view.phtml/aid/4465
"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 PrintWithoutSpaces(*args):
output = ""
for i in args:
output = output + i

print output
if __name__ == "__main__":
PrintWithoutSpaces("yo", "hello", "gutentag")
---snip----

this prints "yohellogutentag"

--
__________________________________________________ _______________
Bjoern Paschen ._--_. Panasonic AVC Networks Germany GmbH
pa*****@mavd.de -- Audio Video Technology Centre
Jul 18 '05 #2

P: n/a
> def PrintWithoutSpaces(*args):
output = ""
for i in args:
output = output + i

print output
if __name__ == "__main__":
PrintWithoutSpaces("yo", "hello", "gutentag")
---snip----

this prints "yohellogutentag"


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

PrintWithoutSpaces("a", 10)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in PrintWithoutSpaces
TypeError: cannot concatenate 'str' and 'int' objects
A better way would be this:

def myprint(*args):
print "".join([str(x) for x in args])
Jul 18 '05 #3

P: n/a
> 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?

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

import sys
sys.stdout.write('%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:
def PrintWithoutSpaces(*args):
output = ""
for i in args:
output = output + i

print output
if __name__ == "__main__":
PrintWithoutSpaces("yo", "hello", "gutentag")
---snip----

this prints "yohellogutentag"


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

PrintWithoutSpaces("a", 10)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in PrintWithoutSpaces
TypeError: cannot concatenate 'str' and 'int' objects
A better way would be this:

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


Jul 18 '05 #4

P: n/a


Dan Williams wrote:
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

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.
a="a"
b="b"
print "%s%s" % (a,b) ab


Jul 18 '05 #5

P: n/a
Hello Dan,
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?

I use the % formatting and find it much better.

HTH.
Miki
Jul 18 '05 #6

P: n/a
On Thu, 05 Feb 2004 14:17:05 +0100, Diez B. Roggisch wrote:
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])

Thanks. Works like a charm :)
--
__________________________________________________ _______________
Bjoern Paschen ._--_. Panasonic AVC Networks Germany GmbH
pa*****@mavd.de -- Audio Video Technology Centre
Jul 18 '05 #7

P: n/a
In article <bv************@ID-111250.news.uni-berlin.de>,
"Diez B. Roggisch" <no**********@web.de> wrote:
def PrintWithoutSpaces(*args):
output = ""
for i in args:
output = output + i

print output
[ ... ]You function won't work on mixed-type args:

PrintWithoutSpaces("a", 10)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in PrintWithoutSpaces
TypeError: cannot concatenate 'str' and 'int' objects
A better way would be this:

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


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.
Jul 18 '05 #8

P: n/a
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!

Jul 18 '05 #9

P: n/a
"Dan Williams" <da*@ithium.net> writes:
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.
Print is a statement, not a function.
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.


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.
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.