A trivial question about print

Max M maxm at mxm.dk
Thu Apr 11 08:35:14 EDT 2002


Alex Martelli wrote:
> Ante Bagaric wrote:

> A vastly superior way is
> to use sys.stdout.write("%s,"%i) -- you just need some care to
> show it off interactively because you need a '\n' at the end,
> so, for example:
> 
> 
>>>>if 1:
>>>
> ...   for i in range(5): sys.stdout.write("%s,"%i)
> ...   sys.stdout.write('\n')
> ...
> 0,1,2,3,4,


A lazy bumm like me would just write:

 >>> print ','.join([str(i) for i in range(5)])
0,1,2,3,4

btw: it would be nice if join() had a switch so that it would 
automatically do a str() on each object it joins.

like::

 >>> print ','.join(range(5), toStr=1)
0,1,2,3,4

I keep having to do this repeatedly in my code when formatting output.

Perhaps join() should even do this as the default, as it makes the most 
sense in the join() context.

like::

def newJoin(self, items):
     return self.join([str(i) for i in items])

Is there any case where it isn't applicable at all?

regards Max M




More information about the Python-list mailing list