Python - Raw String Literals

2020-04-05 19:14发布

问题:

I don't understand how raw string literals work. I know that when using r it ignores all specials, like when doing \n it treats it as \n and not as a new line. but then I tried to do this:

x = r'\'

and it said SyntaxError: EOL while scanning string literal and not '\'

why? did I understanded it correctly? and also what is the explanation for this :

print r'\\' # gives '\\'
print r'\\\' # gives SyntaxError

回答1:

The only way to put in a single quote into a string started with a single quote is to escape it. Thus, both raw and regular string literals will allow escaping of quote characters when you have an unescaped backslash followed by a quote character. Because of the requirement that there must be a way to express single (or double) quotes inside string literals that begin with single (or double) quotes, the string literal '\' is not legal, whether you use a raw or regular string literal.

To get any arbitrary string with an odd number of literal backslashes, I believe the best way is to use regular string literals. This is because trying to use r'\\' will work, but it will give you a string with two backslashes instead of one:

>>> '\\' # A single literal backslash.
'\\'
>>> len('\\')
1
>>> r'\\' # Two literal backslashes, 2 is even so this is doable with raw.
'\\\\'
>>> len(r'\\')
2
>>> '\\'*3 # Three literal backslashes, only doable with ordinary literals.
'\\\\\\'
>>> len('\\'*3)
3

This answer is only meant to complement the other one.



回答2:

In a raw literal the backslash will escape the quote character that is defining the string.

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). 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.

From the docs