Print function and spaces

Rich Krauter rmkrauter at yahoo.com
Thu Feb 5 08:34:19 EST 2004


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




More information about the Python-list mailing list