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 9 2020
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
> 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])
> 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])
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
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
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
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.
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!
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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'...
|
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...
|
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__...
|
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:...
|
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...
|
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".
|
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
...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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...
| |