[Tutor] Printing Multiple Objects, no spaces

David Ascher da@ski.org
Sun, 19 Sep 1999 09:47:52 -0700 (Pacific Daylight Time)


On Sat, 18 Sep 1999, Sean Conway wrote:

> Hello all.
> 
> I need to print multiple objects, without spaces in between. This method
> doesn't work:
> 
> print foo, foobar

You need to use the sys.stdout.write() method directly, and build your
strings yourself.  For example:

sys.stdout.write(str(foo))
sys.stdout.write(str(foobar))

Or more generically:

objects = [foo, foobar]  # put whatever you want here
object_strings = map(str, objects)
onebigstring = string.join(object_strings, '') # join w/o spaces
sys.stdout.write(onebigstring)

Or more concisely and harder-to-read:

sys.stdout.write(string.join(map(str, objects), ''))

BTW, that won't write the last newline, which you can just add with a
+'\n' after the string construction.

--david