7

Also see the Why can't I end a raw string with a backslash? and Why can't Python's raw string literals end with a single backslash? questions and related answers.


In my Python 2 program I use a lot of literal strings with embedded backslashes. I could use another backslash to escape each of these backslashes (eg: "red\\blue") or use Python raw strings (eg: r"red\blue"). I have standardised on the raw string method which works well in all cases except one.

If I want to represent a double backslash, I can use r"\\", but if I try to enter a single backslash literal r"\" Python complains with a syntax error. The obvious workaround is to use "\\" in this case, but why is the single raw string backslash an error? Is this a bug in Python? Is there a way to code a single backslash as a raw string?

Example:

>>> r"red\blue"
'red\\blue'
>>> r"\\"
'\\\\'
>>> r"\"
  File "<stdin>", line 1
    r"\"
       ^
SyntaxError: EOL while scanning string literal
>>> 

I would rather be consistent using raw strings through-out the whole program, and using this "\\" in several places seems like a kludge. Using r"\\"[0] is not any better. I have also considered using a constant BACKSLASH where BACKSLASH = r"\\"[0] at the start of the program. Another kludge.

UPDATE: This error also happens when there is an odd number of backslashes at the end of a raw string. The default string scanner that is used interprets the backslash as an escape character, so that the last backslash will escape the ending quote character. It was intended that " and ' can be embedded in the string, however the resulting string will still have the backslashes inside as a plain character.

There are several questions related to this issue but none of the answers explains why the single backslash raw string is an error or how to code a single backslash as a raw string:

python replace single backslash with double backslash

can't print '\' (single backslash) in Python

Python regex to replace double backslash with single backslash

Convering double backslash to single backslash in Python 3

Why can't I end a raw string with a backslash?

Why can't Python's raw string literals end with a single backslash?

Community
  • 1
  • 1
Logic Knight
  • 289
  • 4
  • 10
  • 2
    because the single backslash acts like an escape char for the following double quotes. Now it consider that there isn't an ending quote. – Avinash Raj May 17 '15 at 03:35
  • The best way I've found of including a single \ in a raw string literal is to use string formatting: `r"%s" % "\\"` will yield: `'\\'`. – MattDMo May 17 '15 at 03:44
  • 1
    @AvinashRaj Thanks for your comment. I am still struggling to understand why a single backslash is not allowed if the 'r' prefix disables the function of the backslash and treats just like any other character. Perhaps the backslash retains some function. Hmmm. – Logic Knight May 17 '15 at 03:45
  • 1
    @CarpetPython The last Q&A you linked to in your question ([Why can't I end a raw string with a backslash?](http://stackoverflow.com/questions/11168076/why-cant-i-end-a-raw-string-with-a-backslash)) seems to have your answer. There are still escape sequences with backslashes in raw strings, just less of them. The `\"` in `r"\"` is one. – Jonathan Lonowski May 17 '15 at 13:10
  • @JonathanLonowski, the Q&A tells us that an odd number of backslashes at the end of a raw string causes the error. It does not explain *why* this design choice was made, and if there is a way to write a single backslash raw string. – Logic Knight May 17 '15 at 13:25
  • @Elisha, the question you have referenced is related to my question but does not address *why* the backlash is still special or *how* to represent the single backslash in a raw string. Please note I have already referenced this link in my question (with 5 other similar questions). I already know the work-arounds, but I was hoping somebody familar with the internals of the Python parser would respond. – Logic Knight May 19 '15 at 05:34
  • @CarpetPython, Ok, I see you referenced it. However, I think it does answer the _why_ question. Maybe it is better to add the _How_ question in the title if it is part of your question. – Elisha May 19 '15 at 05:41
  • @Elisha The title is exactly the question I need and answer to. As answers to this question and related questions do not have this information, I have been researching the issue. It appears there is an acknowledged bug in the Python scanner, but fixing it may break existing code. I intend to write my own answer to this question in case other programmers need the information. – Logic Knight May 19 '15 at 06:15
  • 2
    The duplicate links to the FAQ at https://docs.python.org/3/faq/design.html#why-can-t-raw-strings-r-strings-end-with-a-backslash though I agree that it is surprising and odd that `r'\'` doesn't work like you would expect. – tripleee Sep 09 '16 at 10:39
  • "There are several questions related to this issue but none of the answers explains why the single backslash raw string is an error" Yes, they do. The single backslash raw string is an error because it is a string that *ends with* a single backslash (or at least is intended to), and the canonical covers the problem there in great detail. "or how to code a single backslash as a raw string" They do not say how to do this, because it cannot be done. – Karl Knechtel Aug 04 '22 at 20:24
  • Some of the other questions here are clearly irrelevant. https://stackoverflow.com/questions/14453152/ for example completely misunderstands the difference between the string contents and string representation, and how backslash-escaping works in general. – Karl Knechtel Aug 04 '22 at 20:27

1 Answers1

1

The problem here is simple, the system assumes that the single backslash is for escaping, you are escaping your quote, otherwise you have to escape the escape char. This would happen in any environment that lets a character disable another characters function.

Zach S.
  • 130
  • 1
  • 9
  • 3
    Thanks Zach. In a raw string where the backslash should be just an ordinary character, why does it escape the end quote? – Logic Knight May 17 '15 at 04:01
  • Repeating what @LogicKnight has asked: Why is the backslash escaped when the whole purpose of a raw string is to be raw, in other words to have no escaping? – Reversed Engineer Oct 15 '21 at 10:17
  • And `print (r'What\'s in a name?')` produces `What\'s in a name?`, so no escaping is going on here! Clearly, the system isn't assuming that the single backslash is for escaping, so the the problem here isn't "simple". – Reversed Engineer Oct 15 '21 at 10:32
  • It's not that the backslash "escapes the end quote"; rather, when Python tries to determine where the closing quote is, it considers that the backslash-quote combination must be part of the string and that the quote cannot be the end of it yet. This is because, when the code is compiled, answering "what part of this code is the string literal?" happens in a *separate* step that comes *before* "what text is in the string represented by this string literal?". – Karl Knechtel Aug 05 '22 at 10:38