-1

As far as I thought, the following should work, however I get:

Python 3.4.5 (default, Nov  9 2016, 16:24:59)
[GCC 4.8.5 20150623 (Red Hat X.X.X-XX)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> '\\'
'\\'
>>> '\'
  File "<stdin>", line 1
    '\'
      ^
SyntaxError: EOL while scanning string literal

Am I missing something?

I'm expecting '\\' to return \.

category
  • 2,113
  • 2
  • 22
  • 46
  • 2
    why should it? You are escaping the quote and the string is missing its closing quote. – Ma0 Mar 23 '17 at 12:36
  • 1
    I'm not sure what you are asking. `\'` is an escaped quote, *not* the end of the string. – Martijn Pieters Mar 23 '17 at 12:36
  • 3
    And don't confuse the representation with the value! `'\\'` is the valid Python syntax to produce that value, so that's what Python echoes again. If it echoed `'\'` and you tried to copy and paste that you'd get the exact same `SyntaxError` exception you just created yourself! – Martijn Pieters Mar 23 '17 at 12:37
  • So your question is how to print just one slash? (`'\'`) – Ma0 Mar 23 '17 at 12:38
  • 3
    Use `print('\\')` to see that the *value* contains just one character. Or use `len('\\')`. Or `'\\'[0]'`. – Martijn Pieters Mar 23 '17 at 12:38

1 Answers1

2

I'm expecting '\\' to return \.

It does, if you print it.

At the interactive prompt it displays the repr to show you it is a string with a literal backslash in it. This is for debugging. It'll show you '\n' instead of printing a blank line, too. print it and get a blank line.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251