473,772 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2049
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 PrintWithoutSpa ces(*args):
output = ""
for i in args:
output = output + i

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

this prints "yohellogutenta g"

--
_______________ _______________ _______________ _______________ _____
Bjoern Paschen ._--_. Panasonic AVC Networks Germany GmbH
pa*****@mavd.de -- Audio Video Technology Centre
Jul 18 '05 #2
> def PrintWithoutSpa ces(*args):
output = ""
for i in args:
output = output + i

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

this prints "yohellogutenta g"


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

PrintWithoutSpa ces("a", 10)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in PrintWithoutSpa ces
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.writ e('%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 PrintWithoutSpa ces(*args):
output = ""
for i in args:
output = output + i

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

this prints "yohellogutenta g"


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

PrintWithoutSpa ces("a", 10)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in PrintWithoutSpa ces
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**********@w eb.de> wrote:
def PrintWithoutSpa ces(*args):
output = ""
for i in args:
output = output + i

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

PrintWithoutSp aces("a", 10)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in PrintWithoutSpa ces
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
4174
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' anything similar you like. Would make my life so much easier. Often I start with a nice and short print statement, then there happen to be more arguments and neat formatting becomes more important and as a result I find myself
9
22174
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 end? C:\src\projects\test1>python -c "import sys;print sys.version, 'running on', sys.platform" 2.3.4 (#53, May 25 2004, 21:17:02) running on win32 C:\src\projects\test1>python -c "print 'here'," >jjj
12
2406
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
2917
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 $
2
2888
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 a sequence of spaces; recursive function 2 uses input to display ascending sequence of digits; likewise, recursive function 3 uses input to display descending sequence of digits. I have not followed the instructions completely regarding the...
5
48361
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
1832
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 There is also a wiki page that collects the ideas: http://wiki.python.org/moin/PrintAsFunction There is the will to keep the printing operation very simple for the most common situation, but at the same time to make it flexible (optional no...
3
2109
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 improvement. And of course it's possible there's some serious "gotcha" I've overlooked. Thus I welcome any and all comments. If there's some agreement that this proposal is worth further consideration then I'll re-submit a formal document in...
3
2620
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 Explorer...Print part? I can create the HTML file, just need help with calling the Explorer...Print function - supplying the path to the file so that only the Print Dialog pops up once they click on "Print" Thanks!
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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
10103
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
10038
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
9911
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
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
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.