[Tutor] Newline question
Alan Gauld
alan.gauld at btinternet.com
Sat Aug 4 00:12:03 CEST 2012
On 03/08/12 22:09, Alexander Q. wrote:
> That was it Jerry- when I typed in "print hello" instead of just
> "hello", the output was exactly like the one in the tutorial.
Yes, the interactive interpreter shows the representation
(repr()) of the data while print shows the normal output. There are
several places where this difference is significant.
> Alternatively, I could accomplish the same type of output by using
> triple quotes around the same text, except that I would have to format
> it manually if I want it to come out looking the same
Nope, that won;t work either, try it:
>>> s = """Here is
... a string
... with
... line
... breaks"""
>>> s
'Here is \na string\nwith\nline\nbreaks'
>>> print s
Here is
a string
with
line
breaks
>>>
Again repr() just prints the newline characters.
> I understand it from your explanation is that "hello" does a literal
> output of everything typed without processing the escape backslashes,
> while "print hello" does process them.
Yes, and realise that
>>> hello
only works at the interactive >>> prompt. If you write a program in a
file and run it typing hello on a line by itself with have no result.
Python will quietly evaluate the string but not display it. The ability
to display a value is a debugging aid only available in the interpreter
prompt. In a program you need to explicitly call repr(hello)
This is why in my tutorial I never actually tell the users about the
hello form but always ask them to type the print... More typing but more
consistent output, so ultimately less confusion for beginners.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list