[Tutor] Interactive escape sequences

Alan Gauld alan.gauld at btinternet.com
Tue Jan 14 10:22:04 CET 2014


On 14/01/14 07:42, Christian Alexander wrote:

> Why does the interactive prompt not recognize escape sequences in
> strings?  It only works correctly if I use the print function in python 3.
>
>  >>> "Hello\nWorld"
> "Hello\nWorld"

That depends on how you define "correctly"...

When you evaluate an expression at the Python prompt Python prints the 
repr() of the value. For strings that includes the quotes and the \n 
characters  and any other special characters it finds. The print 
function on the other hand prints the str() of the value and that 
interprets the quotes etc out

In general repr() is more useful for debugging since it shows any 
'hidden' whitespace characters. repr() by convention should return a 
value that can be evaluated and assigned to a variable, although it 
doesn't always do that for miore complex types.

hth
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list