is this a Python bug?

Anthony J Wilkinson anthony at
Wed Feb 16 09:38:58 EST 2000

On 16 Feb 2000, Brian Langenberger wrote:
> piet at wrote:
> :>>>>> Moshe Zadka <moshez at> (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

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

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


Anthony J Wilkinson                                        anthony at
Software Engineer                                 
DSTC Pty Ltd                                           Ph:  +61 7 3365 4310

More information about the Python-list mailing list