Why can’t I end a raw string with a backslash? [duplicate]

You still need \ to escape ' or " in raw strings, since otherwise the python interpreter doesn’t know where the string stops. In your example, you’re escaping the closing '.

Otherwise:

r'it wouldn\'t be possible to store this string'
r'since it'd produce a syntax error without the escape'

Look at the syntax highlighting to see what I mean.

Leave a Comment