printing with print

Fredrik Lundh Fredrik.Lundh at p98.f112.n480.z2.fidonet.org
Wed Jun 30 10:37:10 CEST 1999


From: "Fredrik Lundh" <fredrik at pythonware.com>

Angus MacKay wrote:
> is there any other way to print that with print?
> perhaps with a real function.
> 
> I want to print two things without a space being added between them:
> foo = "there"
> print "hi" + foo
> 
> works fine but: 
> foo = 2
> print "hi" + foo
> 
> does not. I like the printing of any object ability of "print" so I
> do not want to use "" % style printing, I just want to be able to
> govern the whitespace that gets output.

note that the "s" formatting operator can handle any non-tuple
object, just like print (both use "str" to convert non-string objects
to strings).

the following two statements give the same result, as long as
foo is not a tuple.

    print foo
    print "%s" % foo

to deal with tuples too, you can call str yourself, or wrap the
object in a singleton tuple:

    print "%s" % str(foo)
    print "%s" % (foo,)

</F>




More information about the Python-list mailing list