Why does >>> 'c\\\h'
produces 'c\\\\h'
via the python CLI
But >>> print 'c\\\h'
produces c\\h
Why does >>> 'c\\\h'
produces 'c\\\\h'
via the python CLI
But >>> print 'c\\\h'
produces c\\h
Python interpreter running in REPL mode prints representation (repr
builtin) of result of last statement (it it exists and not a None
):
>>> 5 + 6
11
For str
objects representation is a string literal in a same form it is written in your code (except for the quotes that may differ), so it includes escape sequences:
>>> '\n\t1'
'\n\t1'
>>> print repr('\n\t1')
'\n\t1'
print
statement (or function) on the other hand prints pretty string-conversion (str
builtin) of an element, which makes all escape sequences being converted to actual characters:
>>> print '\n\t1'
<---- newline
1 <---- tab + 1