0

For example, if I have

myStr = r'D:\@Music\Classical\Scriabin'

then

print myStr[myStr.rfind('\\'):]

works fine. I thought it would be equivalent to

print myStr[myStr.rfind(r'\'):]

but the latter expression raises an EOL exception.

Could anybody tell me why? The r should escape the single backslash or am I misunderstanding the whole thing altogether?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
RubenGeert
  • 2,902
  • 6
  • 32
  • 50
  • 1
    Refer the last part. r'\' not a valid string https://docs.python.org/2.0/ref/strings.html – Shiva Nov 27 '15 at 19:39
  • Thx! I see. It's just a really odd exception. – RubenGeert Nov 27 '15 at 19:44
  • Yes it is... I was baffled by question n i dearly wanted answer. But the explanation made sense. That odd no. of \ will b treated as escape character n not string – Shiva Nov 27 '15 at 19:47
  • Well, it says "Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character)." However, I think the backslash would *not* escape the quote because the backslash itself has been escaped by the `r`. – RubenGeert Nov 27 '15 at 19:52
  • r'\' can be Interpreted as r escaping first ' and \ escaping second ' hence no valid string – Shiva Nov 27 '15 at 19:55
  • Now i have confused myself. – Shiva Nov 27 '15 at 19:58
  • Ha ha!! Well, as long as we get our jobs done, it's all good, right? – RubenGeert Nov 27 '15 at 20:15
  • Yes :D it is of course true "it's easy to learn Python but not to use" ;) – Shiva Nov 28 '15 at 17:10

0 Answers0