Print function and spaces

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


Well, in the end I wrote this:


def echo(*args):
	if (len(args) == 0):
		sys.stdout.write("\n")
		return
	
	for x in args:
		if (x != None):
			sys.stdout.write(str(x))

	if (x != None):
		sys.stdout.write("\n")


I called it echo for obvious reasons !-)

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

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

Dan


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

 

-----Original Message-----
From: python-list-bounces+dan=ithium.net at python.org
[mailto:python-list-bounces+dan=ithium.net at python.org] On Behalf Of Diez B.
Roggisch
Sent: 05 February 2004 13:17
To: python-list at python.org
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])


--
http://mail.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list