Print function and spaces

D

Dan Williams

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
 
D

Diez B. Roggisch

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])
 
R

Rich Krauter

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

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])
 
B

Bjoern Paschen

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/developerworks/library/l-python101.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"
 
W

wes weston

Dan said:
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.
 
M

Miki Tebeka

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
 
M

Mel Wilson

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.
 
P

Paul Miller

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!
 
B

Bjoern Paschen

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 :)
 
P

Paul Rubin

Dan Williams said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top