This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: raw strings
Type: compile error Stage:
Components: Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: 846909323, amaury.forgeotdarc, r.david.murray
Priority: normal Keywords:

Created on 2011-04-07 08:48 by 846909323, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg133199 - (view) Author: 846909323 (846909323) Date: 2011-04-07 08:48
>>> print(r'\')
      
SyntaxError: EOL while scanning string literal
>>> print(r'\'')
\'
>>>
msg133201 - (view) Author: 846909323 (846909323) Date: 2011-04-07 09:13
I think it should be

    >>> print(r'\')
    \
    >>> print(r'\'')

    SyntaxError: EOL while scanning string literal
    >>>
msg133203 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-04-07 09:36
This is by design and documented:
http://docs.python.org/reference/lexical_analysis.html

"""
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.
"""
msg133210 - (view) Author: 846909323 (846909323) Date: 2011-04-07 12:05
Sorry for my poor english and thank you for the answer.
Since I'm a perler, I think this is counterintuitive.
(In perl:
    print '\';  #print \
    print '\''; #error
    print "\""; #print "
    print "\";  #error
)
msg133278 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-04-08 00:09
To a pythonista, the perl behavior is counter-intuitive :)

That said, the behavior of r'\' *is* somewhat counter-intuitive.  See issue 1271 for a fairly thorough exploration of why it is the way it is.
History
Date User Action Args
2022-04-11 14:57:15adminsetgithub: 56002
2011-04-08 00:09:27r.david.murraysetnosy: + r.david.murray
messages: + msg133278
2011-04-07 12:05:52846909323setmessages: + msg133210
2011-04-07 09:36:46amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg133203

resolution: not a bug
2011-04-07 09:13:49846909323setmessages: + msg133201
2011-04-07 08:48:34846909323create