For a neatish way to get a string to end with a single backslash, how about mystr = r"abc\ "[:-1] (Note the space at the end of the rough-quoted string.)
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> Virus-free. www.avast.com <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> On Mon, Oct 19, 2020 at 9:26 AM Antoon Pardon <antoon.par...@vub.be> wrote: > > > Op 13/10/20 om 15:14 schreef Serhiy Storchaka: > > 13.10.20 11:52, Tony Flury via Python-list пише: > >> I am trying to write a simple expression to build a raw string that ends > >> in a single backslash. My understanding is that a raw string should > >> ignore attempts at escaping characters but I get this : > >> > >> >>> a = r'end\' > >> File "<stdin>", line 1 > >> a = r'end\' > >> ^ > >> SyntaxError: EOL while scanning string literal > >> > >> I interpret this as meaning that the \' is actually being interpreted as > >> a literal quote - is that a bug ? > > > > r'You can\'t end raw string literal with a single "\"' > > > > If backslash be true inner in a raw string, the above literal would end > > after \'. It would be very hard to write a raw string containing both \' > > and \", and even \''' and \""" (there are such strings in the stdlib). > > > > So you have problem either with trailing backslash, or with inner > > backslash followed by quotes. Both problems cannot be solved at the same > > time. Python parser works as it works because initially it was easier to > > implement, and now this cannot be changed because it would break some > > amount of correct code. > > IMO the way python does this is broken. > > >>> st=r'You can\'t end raw string literal with a single "\"' > >>> print(st) > > Now either the \ is special or it is not. > > 1) If it is special, it should change how the ' is treated but not > appear itself. > > 2) If it is not special, it should just appear and not change how the ' > is treated. > > What python does here is a combination of both. The \ appears and it > changes how the ' is treated. That is IMO broken. > > -- > Antoon Pardon. > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list