0

The current Python grammar doesn't allow one to output a trailing \ in a raw string:

>>> print(r'a\b\c\')

SyntaxError: EOL while scanning string literal

On the contrary, you can write Bash like this:

echo 'a\b\c\'

I understand what the doc is saying. I wouldn't feel strange if an expression '\' fails because the backslash is escaping the quote. What I'm questioning is r'\': Aren't raw strings meant to be raw (which means backslashes in the string are taken literally)?

Do we have to write r'a\b\c' + '\\' or 'a\\b\\c\\' to make a string literal a\b\c\ in Python? I couldn't see how this is Pythonic.

Cyker
  • 9,946
  • 8
  • 65
  • 93
  • 2
    because you are escaping `'` there and your string is incomplete without the ending single-quote – Subramanya Vajiraya Dec 10 '16 at 11:09
  • 1
    @SubramanyaVajiraya Doesn't the prefix `r` mean the string should be interpreted verbatim?. Why would the backslash take effect here? – Cyker Dec 10 '16 at 11:33

3 Answers3

4

From the documentation,

Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.

Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
  • Is that because the parser cannot handle `'\'`? Raw strings are meant to be raw, but this looks like a deficiency. – Cyker Dec 10 '16 at 11:36
  • Python's lexical analysis is defined to require its input to escape backslashes, as (again) pointed out in the documentation. – Michael Foukarakis Dec 10 '16 at 11:40
  • Do you mean the grammar listed [here](https://docs.python.org/2/reference/lexical_analysis.html#string-literals)? Could you please update your answer with the paragraphs pointing out what you said here? – Cyker Dec 10 '16 at 11:44
  • Everything I've said is in the same link contained in my answer. Please read it. – Michael Foukarakis Dec 10 '16 at 11:45
  • The most relevant part I can find is the CFG: `shortstringitem ::= shortstringchar | escapeseq`. If this is what you mean then it looks like a deficiency. There may be a better grammar for raw strings. – Cyker Dec 10 '16 at 11:48
1

The limitation is due to the fact that you need someway to include a ' inside a raw string. Otherwise there is no way to put bob said "I'm not hungry" in a string.

So you end up in weird situation where you need an escape character for this case. So in raw strings you escape a ' with a \ and yes the \ stays in the string.

So r'bob said "I\'m not hungry"' it is!!

Nath
  • 748
  • 4
  • 16
  • `raw` is for the convenience of backslashes. If you want to include single/double quotes then you should use triple quotes. Unfortunately in the case I pointed out in question, `raw` isn't completely convenient for backslashes. – Cyker Dec 10 '16 at 12:19
  • Raw is normally so you can put backlashes in them without them being interpreted by the parser part of an escape sequence. But as my answer points out you still need an a way to escape quotes. Triple quoting solves most use cases but there will still be times where you need to encode a triple quote in string and so he problem remains. Having an escape character is still required. – Nath Dec 10 '16 at 12:28
0

When you write print(r'\'), Python understand \' in that statement as a character. Because of that python raised syntax error because the there is a incomplete string inside print function. For an example if you need to print i am "free" man , you should write

print("i am \"free\" man")
Vibhutha Kumarage
  • 1,372
  • 13
  • 26
  • In raw strings Python understands `\'` as two characters ``\`` and `'`. Backslash just causes the next character to be treated as part of the string but the backslash itself is also part of the string. --- `>>> r'I\'m hungry.'` -> `"I\\'m hungry."` --- Good explanation: [Why can't Python's raw string literals end with a single backslash?](https://stackoverflow.com/a/19654184/320437) – pabouk - Ukraine stay strong May 20 '22 at 11:15