is this a Python bug?

Anthony J Wilkinson anthony at dstc.edu.au
Wed Feb 16 15:38:58 CET 2000


On 16 Feb 2000, Brian Langenberger wrote:
> piet at cs.uu.nl wrote:
> :>>>>> Moshe Zadka <moshez at math.huji.ac.il> (MZ) writes:
[...]
> : MZ> Raw strings cannot end with a backslash. 
> 
> : They can. But only with an even number of them.
> 
> I'm assuming there's some clever and important reason for this.
> But for the life of me I can't figure out what it is.
> Could someone enlighten me as to the reason? :)

Every second backslash is 'escaped' by the one before it (as with normal
strings) therefore if your string ends in an odd number of backslashes
then the odd backslash will escape the end quote which creates a syntax
error:

>>> r'\\'
'\\\\'
>>> r'\'
  File "<stdin>", line 1
    r'\'
       ^
SyntaxError: invalid token

>>> r'abc\\'
'abc\\\\'
>>> r'abc\'
  File "<stdin>", line 1
    r'abc\'
          ^
SyntaxError: invalid token

Regards,
Anthony

___________________________________________________________________________
Anthony J Wilkinson                                        anthony at dstc.com
Software Engineer                                           http://dstc.com
DSTC Pty Ltd                                           Ph:  +61 7 3365 4310






More information about the Python-list mailing list