0

I am curious to know why?

r"\\" print \\\\

R.A.Munna
  • 1,699
  • 1
  • 15
  • 29
  • The backslash "escapes" the quote character that would otherwise close the string. What you are left with is an unclosed string and python is expecting the string to terminate - not the file to end. – Lix May 25 '17 at 11:01
  • In addition - for the future - it would by MUCH easier to just copy and paste the code instead of taking a screenshot, uploading it and then pasting the an image of text. – Lix May 25 '17 at 11:02
  • @Lix, Thanks and I will keep in mind your suggestion. – R.A.Munna May 25 '17 at 11:17

1 Answers1

1

Don't confuse with the immediate output of python and print statements.
Hopefully, The following example will clarify your doubts.

In [5]: a = "a\nb"
In [6]: a
Out[6]: 'a\nb'
In [7]: print a
a
b
In [8]: a = r'\\'
In [9]: a
Out[9]: '\\\\'
In [10]: print a
\\

If your doubt is regarding raw string (r'' represents raw string), this is a good read.
Quoting the essentials here:

A "raw string literal" is a slightly different syntax for a string literal, in which a backslash, \, is taken as meaning "just a backslash" (except when it comes right before a quote that would otherwise terminate the literal) -- no "escape sequences" to represent newlines, tabs, backspaces, form-feeds, and so on. In normal string literals, each backslash must be doubled up to avoid being taken as the start of an escape sequence.

Jithin Pavithran
  • 1,250
  • 2
  • 16
  • 41