6

Just a quick silly question. How do I write a trailing slash in a raw string literal?

r = r'abc\'  # syntax error
r = r'abc\\' # two slashes: "abc\\"
gog
  • 10,367
  • 2
  • 24
  • 38
  • You cannot. Simple as that. Don't use a raw string. Use a normal string. – sshashank124 Apr 29 '14 at 09:34
  • @thefourtheye and others: I think "*how* do I do this" is a sufficiently different (and much more practical) question than "*why can't* I do this". Of course, I may be biased because I already wrote an answer :-) –  Apr 29 '14 at 09:37
  • 3
    @delnan Which has already been covered in both the referenced duplicates. – devnull Apr 29 '14 at 09:44
  • documentations of this issue: https://docs.python.org/2/faq/design.html#why-can-t-raw-strings-r-strings-end-with-a-backslash – Elisha Apr 29 '14 at 09:44
  • Also http://stackoverflow.com/questions/2870730/python-raw-strings-and-trailing-backslash – devnull Apr 29 '14 at 09:45
  • @devnull Ah yes, a few more answers down. Should have looked more closely. –  Apr 29 '14 at 09:54
  • @delnan And I'm sure you haven't read the [faq](https://docs.python.org/2/faq/design.html#why-can-t-raw-strings-r-strings-end-with-a-backslash) either. – devnull Apr 29 '14 at 09:55
  • @devnull I have, but the rest of the internet has no bearing on whether an Stackoverflow question is considered a duplicate. And I can't exactly [just link to the FAQ](http://meta.stackexchange.com/q/8231). –  Apr 29 '14 at 09:57
  • @delnan Oh well, if you feel that a question that has been answered scores of times should be answered again, feel free as you've already done. After all, [it's the norm](http://meta.stackoverflow.com/questions/252506/question-quality-is-dropping-on-stack-overflow). – devnull Apr 29 '14 at 10:06
  • @devnull No need to be passive aggressive, neither to me nor towards OP. And FYI, I wrote an answer because I did not realize it was already answered, I'm quick with the close votes myself. I even freaking cast the final close vote on this very question after you pointed out my mistake! –  Apr 29 '14 at 10:11
  • @delnan Yes, I can [see that](http://stackoverflow.com/questions/23312571/fast-xoring-bytes-in-python-3/) without much effort. Don't bother to respond. – devnull Apr 29 '14 at 10:13

2 Answers2

7

You can't. A raw string literal can't end with an odd number of backslashes (langref; last paragraph of that section). You can, howerver, write a raw string literal without the backslash, and write the final backslash as an ordinary string literal:

r = r'abc' '\\'

Adjacent string literals are implicitly concatenated by the parser.

  • +1 I thought it is not possible... – thefourtheye Apr 29 '14 at 09:41
  • 1
    @thefourtheye http://stackoverflow.com/a/11170170/2235132 Also http://stackoverflow.com/a/5830053/2235132 – devnull Apr 29 '14 at 09:42
  • @thefourtheye It's not possible to write (in python syntax, not in string literal AST node) *one* raw string literal ending in an odd number of backslashes. Once you lift the restriction about the number of string literals and their types the question becomes "how to concatenate two strings" which is trivial... – Bakuriu Apr 29 '14 at 09:47
2

Raw string literals are parsed in exactly the same way as ordinary string literals; it’s just the conversion from string literal to string object that’s different. This means that all string literals must end with an even number of backslashes; otherwise, the unpaired backslash at the end escapes the closing quote character, leaving an unterminated string.

Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81