print without newline?
Alex Martelli
aleaxit at yahoo.com
Sat Sep 2 09:52:04 EDT 2000
More information about the Python-list mailing list
Sat Sep 2 09:52:04 EDT 2000
- Previous message (by thread): EXTREME NOOB, lists?
- Next message (by thread): print without newline?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Ian Hobson" <ian.hobson at ntlworld.com> wrote in message news:nbnhlPA$UEs5Ew4W at ntlworld.com... [snip] > >>Is there a way to print without the newline (carriage return > >>or line feed)? This does not do what I want: > >> print "Hello.", > >>because it puts a space after the "Hello." > > > >Actually, it doesn't put a space after the "Hello.", the next print > >statement puts a space before its output. > > I'm sure there is logic behind this choice, but it escapes me. > Can anyone explain why print should put a space out first? The print statement is (was) meant mainly as a convenience for beginners & rapid insertion of "let's see what it's doing here" in a program that's behaving strangely. If it didn't use spaces by default, then if you had print x, and later print y, and saw the output 1234 you'd have no way to know whether x was 1 and y 234, or x was 12 and y was 34. The space-by-default helps. > And can they confirm that the FIRST print in the program does not. Just bind sys.output.softspace to 0 before any print, and you can be sure it won't emit the initial space. E.g. interactively: >>> print 12, ; print 34 12 34 >>> print 12, ; sys.stdout.softspace=0 ; print 34 1234 >>> This of course suggests the usual kind of wrapping-object trickery, e.g....: class nospaces: def __init__(self,afile): self.afile=afile def __getattr__(self,attr): if attr=='softspace': return 0 else: return getattr(self.afile,attr) def __setattr__(self,attr,value): if attr=='softspace': return else: self.__dict__[attr]=value >>> sys.stdout=Script1.nospaces(sys.stdout) >>> print 12, ; print 34 1234 >>> ...but, resist the temptation: entertainment value apart, using sys.stdout.write(str(foo)) (you can of course easily wrap that in a function) is really simpler and more effective than softspace trickery, IMHO. Alex
- Previous message (by thread): EXTREME NOOB, lists?
- Next message (by thread): print without newline?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list