[Tutor] simple list question
John Fouhy
john at fouhy.net
Mon Feb 20 23:58:05 CET 2006
On 21/02/06, Ara Kooser <ghashsnaga at gmail.com> wrote:
> Why is that? I thought that adding , after the print command would allow
> the format to stay the same. Is there a better way of doing this (I like
> lists because I can edit them easily)? Thanks.
A comma after a print statement basically replaces the newline with a space.
Originally, I guess, this was just so that you could say 'print x, y, z'.
>>> print 'foo', 'bar', 'baz'
foo bar baz
But it means you can also use a trailing comma to suppress the final
newline. eg:
>>> def p():
... print 'foo'
... print 'bar'
... print 'baz'
...
>>> def q():
... print 'foo',
... print 'bar',
... print 'baz'
...
>>> p()
foo
bar
baz
>>> q()
foo bar baz
As to formatting your list --- what exactly are you trying to achieve?
--
John.
More information about the Tutor
mailing list