[Tutor] Interactive escape sequences

Peter Otten __peter__ at web.de
Tue Jan 14 14:18:00 CET 2014


Christian Alexander wrote:

> Hello Tutorians,
> 
> 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"

The way python shows objects in the interactive interpreter has been found 
the most useful for the intended purpose -- to explore python objects and to 
provide a debugging aid. But you can customise the behaviour by defining 
your own displayhook:

>>> import builtins
>>> import sys
>>> def dh(obj):
...     if obj is not None:
...             builtins._ = obj
...             print(obj)
... 
>>> sys.displayhook = dh
>>> "hello\nworld"
hello
world


For a more robust version as a possible starting point for your own efforts 
see:

http://docs.python.org/dev/library/sys.html#sys.displayhook

Personally, I recommend that you use the interactive interpreter as is some 
more -- it'll grow on you. 

You may also give IPython <http://ipython.org/> a try.



More information about the Tutor mailing list