A
alf
question without words:
File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string'\\ '
File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string'\\ '
question without words:
File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string
'\\ '
I wonder if the OP was asking how to spell the one-length string \?
In that case, the answer is that it can't be done using raw strings,
but "\\" does it. Backslash escapes aren't interpreted in raw strings,
but you still can't end a raw string with a backslash.
Jean-Paul
question without words:
File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string
'\\ '
Neil Cerutti said:From the Python Language Reference 2.4.1 String Literals:
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
literal r"\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).
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.
One slash escapes the following character, so the proper way of
writing it is either
r"\\" or r"\""
a) That is weird! Why would you ever want to do that - ie
insert \" into your string as a special case? If I wanted a "
in a raw string then I'd use a r'' string or a triple quoted
string.
If we removed a) then we could remove b) also and r"" strings
would work as everyone expects.
Does anyone know the justification for a)? Maybe we should
remove it in py3k?
If the escaped quotes didn't function in raw strings, I'd be
unable to construct (with a single notation) a regex that
included both kinds of quotes at once.
re.compile(r"'\"")
Nick Craig-Wood said:Does anyone know the justification for a)? Maybe we should remove it
in py3k?
Where's the problem!? ::
re.compile(r''''"''')
That incurs the problem that you can't end with a '. So you
can't end with *something* either way.
Marc 'BlackJack' Rintsch said:Where's the problem!? ::
re.compile(r''''"''')
Ah, I see -- readability is the problem.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.