I was under the impression that in Python a raw string, written as r'this is a raw string'
would omit any escape characters, and print EXACTLY what is between the quotes. My problem is that when I try print r'\'
I get SyntaxError: EOL while scanning string literal
. print r'\n'
correctly prints \n
, though.

- 1,406
- 2
- 17
- 37
2 Answers
Quoting the documentation:
When an
'r'
or'R'
prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literalr"\n"
consists of two characters: a backslash and a lowercase'n'
. String quotes can be escaped with a backslash, but the backslash remains in the string; for example,r"\""
is a valid string literal consisting of two characters: a backslash and a double quote;r"\"
is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character).
Added emphasis mine.
Raw strings thus do attach some meaning to a backslash, but only where quotes are concerned.

- 1,048,767
- 296
- 4,058
- 3,343
From the Python docs:
If we make the string literal a “raw” string, \n sequences are not converted to newlines, but the backslash at the end of the line, and the newline character in the source, are both included in the string as data.
You have the wrong idea about raw strings.

- 636
- 1
- 4
- 13