how do i keep python from printing a newline when it exits the program?

Alexander Schmolck a.schmolck at
Mon Oct 11 19:06:56 CEST 2004

"xegfault" <sstaats at> writes:

> Here is a simple script:
> #!/usr/bin/python
> print "No newline, please.",
> # End of script
> When the program runs, python is still printing a newline when the
> program exits.  How does one keep python from doing so?

class softspaceLess(object):
    >>> import sys
    >>> print "a", "b", " ", "d"
    a b   d
    >>> print >>softspaceLess(sys.stdout), "a", "b", " ", "d"
    ab d
    def __init__(self, file=sys.stdout):
        self.__dict__['_file'] = file
        file.softspace = False
    def __getattr__(self,a):
        if a == 'softspace': return False
        return getattr(self._file,a)
    def __setattr__(self,a,v):
        if a=='softspace': return

More information about the Python-list mailing list