[Tutor] How to print out multiple lines

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 7 Jan 2001 13:53:05 -0800 (PST)

> So that the newline characters are preserved, and when I print them
> out, they are printed to the screen?
> When I retrieved the news articles off the NNTP server, for instance, the
> message body was just one big long string, with some numeric code for the
> newlines. It was just a mess to read.

When you ask the interpreter for a string's value, you'll be getting its
"representation", that is, all the special characters like newlines will
be explicitely shown as escapes:

>>> x = "Hello\nWorld"
>>> x

Python does this because, in many cases, it's useful to see what
particular escapes are within a string.  However, asking for a string's
value isn't quite the same as printing it out:

>>> print x

Use "print".  It will display things as you'd expect.  Also, you can use
stdout.write() with a similar effect.

>>> from sys import stdout
>>> stdout.write(x)

(side note: The main difference between stdout.write and print is that
print will add an extra newline after every call.  Also, print is a
built-in statement --- it's not a function or expression, so it can't be
passed around or used within lambda.  In Scheme terms, its a "special