Print function and spaces

Dan Williams dan at
Sat Feb 7 02:07:15 CET 2004

Well, in the end I wrote this:

def echo(*args):
	if (len(args) == 0):
	for x in args:
		if (x != None):

	if (x != None):

I called it echo for obvious reasons !-)

Basically it emulates print very closely, except it omits that annoying

Maybe it'll be useful to someone other than myself :P


PS - I've used \n because that is what I am used to using. Should I be using


-----Original Message-----
From: at
[ at] On Behalf Of Diez B.
Sent: 05 February 2004 13:17
To: python-list at
Subject: Re: Print function and spaces

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


More information about the Python-list mailing list