1

Why do I see two back slashes when I type variable name on python shell ?

>>> print '6\\3=2'
6\3=2           ==> Correctly prints back slash
>>> var1='6\\3=2'
>>> print(var1)
6\3=2          ==> Correctly prints back slash
>>> var1
'6\\3=2'       ==> Why do I see two back slashes here ? Instead of just one ? 
>>> 

2 Answers2

1

The output in the python shell is the string representation. It is printed in the same way, as one would type it in the code.

Daniel
  • 42,087
  • 4
  • 55
  • 81
1

From the docs:

The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is no equivalent syntax)

print() calls __str__() on the object, while putting the var var1 in the console just calls the objects internal representation __repr__().

>>> print(var1.__str__())
6\2=1
>>> print(var1.__repr__())
'6\\2=1'