print function question
Simon Forman
rogue_pedro at yahoo.com
Tue Jul 25 13:22:04 EDT 2006
Bertrand-Xavier M. wrote:
> On Tuesday 25 July 2006 05:52, Eric Bishop wrote:
> > Why does this work:
> >
> > # start
> > a = 5
> >
> > print a, 'is the number'
> >
> > #end, prints out "5 is the number"
> >
> > But not this:
> >
> > # start
> >
> > a = 5
> >
> > print a 'is the number'
> >
> > #end, errors out
> >
> > The difference here is the comma seperating the variable and the string
> > literal. Is the comma some sort of concatenation operator or is the comma
> > necessary in some form of a requirement in the print function, i.e is the
> > variable a an argument to print as well as 'is th number' another argument
> > to print?
>
> Yes.
> It allows to concat several variables, and also adds a space.
> These do work as well:
>
> a = 5
> print "value is", a
> print "value %s" %(a)
> print "value is", a, '...'
>
> Regards,
> Rob
Also, a comma at the end of a print statement surpresses the usual
trailing newline (it will cause a space to appear instead if you print
something else, but NOT if you write directly to stdout.)
print "Hello",
print "world!"
# prints Hello world! on one line with a space between them, but
import sys
print "Hello",
sys.stdout.write("world!")
# prints Helloworld!
Peace,
~Simon
More information about the Python-list
mailing list