473,231 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Print function and spaces

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
9 2013
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
> 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
> 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


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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Martin Bless | last post by:
Why can't we have an additional 'print' statement, that behaves exactly like 'print' but doesn't insert that damn blank between two arguments? Could be called 'printn' or 'prin1' or 'prinn'...
9
by: Paul Watson | last post by:
I thought that using a comma at the end of a print statement would suppress printing of a newline. Am I misunderstanding this feature? How can I use print and not have a newline appended at the...
12
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
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:...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
5
by: mark | last post by:
how to print, say 50 blank spaces, or say 20 blank spaces ? I wanted use such blank spaces that can be output in a program at predetermined places when printing with "cout".
0
by: bearophileHUGS | last post by:
There is/was a long discussion about the replacement for print in Python 3.0 (I don't know if this discussion is finished): http://mail.python.org/pipermail/python-dev/2005-September/055968.html ...
3
by: James J. Besemer | last post by:
I would like to champion a proposed enhancement to Python. I describe the basic idea below, in order to gage community interest. Right now, it's only an idea, and I'm sure there's room for...
3
by: Andrew Meador | last post by:
I have a button on my form to "Print". I would like to create an HTML file, of the output, and call the Explorer...Print function when this "Print" button is clicked. How can I do this - the...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.