Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple question - end a raw string with a single backslash ?

148 views
Skip to first unread message

Tony Flury

unread,
Oct 13, 2020, 4:53:16 AM10/13/20
to
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 ?

If I try to escaped the backslash I get a different problem:

>>> a = r'end\\'
>>> a
'end\\\\'
>>> print(a)
end\\
>>> len(a)
5
>>> list(a)
['e', 'n', 'd', '\\', '\\']

So you can see that our string (with the escaped backslash)  is now 5
characters with two literal backslash characters

The only solution I have found is to do this :

>>> a = r'end' + chr(92)
>>> a
'end\\'
>>> list(a)
['e', 'n', 'd', '\\']

or


>>> a = r'end\\'[:-1]
>>> list(a)
['e', 'n', 'd', '\\']

Neither of which are nice.



Eryk Sun

unread,
Oct 13, 2020, 5:51:00 AM10/13/20
to
On 10/13/20, Tony Flury via Python-list <pytho...@python.org> wrote:
> 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

Since r'\'' represents a two-character string with a backslash and a
single quote, ending a raw string literal with an odd number of
backslashes requires adding the final backslash using implicit
string-literal concatenation. For example:

>>> r'some\raw\string''\\'
'some\\raw\\string\\'

Other ways to get the same result may operate at runtime instead of at
compile time.

Serhiy Storchaka

unread,
Oct 13, 2020, 10:26:34 AM10/13/20
to
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.

> The only solution I have found is to do this :
>
>     >>> a = r'end' + chr(92)
>     >>> a
>    'end\\'
>     >>> list(a)
>    ['e', 'n', 'd', '\\']
>
> or
>
>
>     >>> a = r'end\\'[:-1]
>     >>> list(a)
>    ['e', 'n', 'd', '\\']
>
> Neither of which are nice.

You can also write r'end' '\\'. It is not too nice, but it looks nicer
to me then two other variants.

Roland Müller

unread,
Oct 15, 2020, 3:17:16 PM10/15/20
to
I used the triple single quotes as delimiter:

>>> s = r'''a single quote ', a double quote "'''
>>> s

'a single quote \', a double quote "'

BR,

Roland


Serhiy Storchaka

unread,
Oct 15, 2020, 3:41:42 PM10/15/20
to
15.10.20 22:16, Roland Müller via Python-list пише:
> I used the triple single quotes as delimiter:
>
>>>> s = r'''a single quote ', a double quote "'''
>>>> s
>
> 'a single quote \', a double quote "'

It does not help if the string contains both kinds of triple quotes

You have to use triple quotes (''', """) for docstrings.

or contains one kind of triple quote and ends with a single quote of
other kind.

a triple single quote ''', a single double quote "

Of course there are workarounds, but the fact that changing the current
rules will break existing code.

Mladen Gogala

unread,
Oct 17, 2020, 5:03:52 PM10/17/20
to
On Thu, 15 Oct 2020 21:30:15 +0000, Stefan Ram wrote:

> Tony Flury <tony....@btinternet.com> writes:
>> >>> a = r'end' + chr(92)
>
> Or maybe,
>
> a = r'''
> end\
> '''[ 1: -1 ]
>
> ? The first and the last line are messy, but in the middle,
> the intended string is clearly visible.


You can use perl module for python. It is installable by pip.
Perl has no problems with handling backslashes.

https://pypi.org/project/perl/

What you need is something like this:

mgogala@umajor:~$ perl -e '$a="abcd";$a=~s/$/\\/; print "$a\n";'
abcd\

Python has a problem with backslashes:

Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> a="abcd"
>>> b=re.sub('$',chr(28),a)
>>> b
'abcd\x1c'

>>> b=re.sub('$',chr(0x41),a)
>>> b
'abcdA'
>>> b=re.sub('$','Z',a)
>>> b
'abcdZ'
>>> b=re.sub('$',chr(0x1C),a)
>>> b
'abcd\x1c'
>>>

Any other character is shown as it should be, except backslash.





--
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com

Peter J. Holzer

unread,
Oct 18, 2020, 10:13:43 AM10/18/20
to
On 2020-10-17 21:03:26 -0000, Mladen Gogala via Python-list wrote:
> On Thu, 15 Oct 2020 21:30:15 +0000, Stefan Ram wrote:
> > Tony Flury <tony....@btinternet.com> writes:
> >> >>> a = r'end' + chr(92)
> >
> > Or maybe,
> >
> > a = r'''
> > end\
> > '''[ 1: -1 ]
> >
> > ? The first and the last line are messy, but in the middle,
> > the intended string is clearly visible.
>
>
> You can use perl module for python.

Ah, I see, that the sillyness of Perl's grammar-altering modules (which
let you write Perl in Latin (with proper declensions and conjugations,
of course) or Chinese) has found its way to Python :-)
0x1C isn't a backslash, it's a control character (FS - File Separator).

0x5C is a backslash, but of course that doesn't work here either,
because the second argument to re.sub isn't a simple string: It contains
escape sequences to be interpreted, and a single \ isn't well-formed
(note that you doubled the \ in your Perl example, too).

b=re.sub('$', '\\\\', a)
or
b=re.sub('$', chr(0x5C)+chr(0x5C), a)

works just fine, as does

b = a + chr(0x5C)

In all these cases,
print(b)
prints
abcd\
(with a single backslash at the end)

But simply typing "b" at the REPL produces
'abcd\\'
because prints the __repr__() of an object. Note that it also prints
single quotes, which are also not part of the string.

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | h...@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
signature.asc

Mladen Gogala

unread,
Oct 18, 2020, 1:07:38 PM10/18/20
to
On Sun, 18 Oct 2020 16:13:16 +0200, Peter J. Holzer wrote:

>
> Ah, I see, that the sillyness of Perl's grammar-altering modules (which
> let you write Perl in Latin (with proper declensions and conjugations,
> of course) or Chinese) has found its way to Python
>

To tell the truth, I only installed it to test whether it works. I am an
old Perl hack forced to learn Python by the market forces. I learned Perl
in 1994 which means that it is my first love of a sort. The fundamental
difference between the two languages is that Perl is procedural while
Python is a fully OO language. Discussion of Perl vs Python necessarily
devolves into the discussion of procedural vs OO paradigms. And that has
been decided long time ago, with the advent of Java over COBOL. Market
has spoken and I had to learn Python. So, I stopped worrying and learned
to love Python. Any resemblance to the subtitle of "Dr. Strangelove" is
purely accidental.
BTW, Visual Studio Code uses pylint to evaluate my adherence to PEP8 and
there is a "kite" extension with autocompletion. Nothing like that was ever
available for Perl. It certainly is easier to write Python scripts than to
write Perl scripts. I do sort of miss $_, @_ and $!. Python's argparse
is supreme and much better than Getopt::Long.

Michael Torrie

unread,
Oct 18, 2020, 2:19:59 PM10/18/20
to
On 10/18/20 11:07 AM, Mladen Gogala via Python-list wrote:
> The fundamental
> difference between the two languages is that Perl is procedural while
> Python is a fully OO language. Discussion of Perl vs Python necessarily
> devolves into the discussion of procedural vs OO paradigms.

Python certainly is procedural. A script starts at the top and executes
through to the bottom and ends, barring any flow control in the middle.
Like Perl you can use it in many different ways and paradigms including
OO if you desire.

Mladen Gogala

unread,
Oct 18, 2020, 7:37:49 PM10/18/20
to
On Sun, 18 Oct 2020 12:19:18 -0600, Michael Torrie wrote:

> Python certainly is procedural. A script starts at the top and executes
> through to the bottom and ends, barring any flow control in the middle.
> Like Perl you can use it in many different ways and paradigms including
> OO if you desire.

That's not the point. In Python, regular expressions are a class. So are strings and lists.
In Perl, there are no classes. Procedural paradigm means that you are modelling the real world
with the objects provided by the computer environment: strings, numbers, lists or hashes (known
as "dictionaries" in Python). OO paradigm allows you to create objects and make them behave like
in the real world. In other words, in OO paradighm, data and functions are related. The basic
idea of the OO was to make code sharing much easier. However, things do not always work as
they should:

https://medium.com/better-programming/object-oriented-programming-the-trillion-dollar-disaster-92a4b666c7c7

https://towardsdatascience.com/object-oriented-programming-is-dead-wait-really-db1f1f05cc44

Of course, not everybody agrees. People never do:

https://techbeacon.com/app-dev-testing/object-oriented-programming-dead-not-long-shot

Sometimes, OO has its funny side:
https://www.wearethemighty.com/articles/that-time-the-australian-air-force-squared-off-against-missile-shooting-kangaroos

Michael Torrie

unread,
Oct 18, 2020, 9:21:23 PM10/18/20
to
On 10/18/20 5:37 PM, Mladen Gogala via Python-list wrote:
> On Sun, 18 Oct 2020 12:19:18 -0600, Michael Torrie wrote:
>
>> Python certainly is procedural. A script starts at the top and executes
>> through to the bottom and ends, barring any flow control in the middle.
>> Like Perl you can use it in many different ways and paradigms including
>> OO if you desire.
>
> That's not the point.

But it is.

Yes I can see why this devolves so quickly for you! :)

Mladen Gogala

unread,
Oct 19, 2020, 2:24:45 AM10/19/20
to
On Mon, 19 Oct 2020 02:44:25 +0000, Stefan Ram wrote:

> Mladen Gogala <mgo...@yahoo.com> writes:
>>In Perl, there are no classes.
>
> If there are no classes in Perl, then what does
>
> bless REF,CLASSNAME
>
> do?

bless \$ref will make the given reference a reference to the class. And classes is Perl
are called "modules". However, Perl classes are not the classes in the real sense. There
is no inheritance. You can have a similar thing in C: it's called "struct", it can do
similar things like Perl modules. But C isn't object oriented. Inheritance is an
essential characteristic of object orientation.
There were attempts to turn Perl into OO language with Moose (https://metacpan.org/pod/Moose)
However, Moose is complex, tedious and rarely used. Perl6 is an OO language, but nobody uses
it. The problem is CPAN and all the Perl5 modules which are not compatible with Perl6 or "Roku"
as it is known.

Chris Angelico

unread,
Oct 19, 2020, 2:53:17 AM10/19/20
to
On Mon, Oct 19, 2020 at 5:26 PM Mladen Gogala via Python-list
<pytho...@python.org> wrote:
> bless \$ref will make the given reference a reference to the class. And classes is Perl
> are called "modules". However, Perl classes are not the classes in the real sense. There
> is no inheritance. You can have a similar thing in C: it's called "struct", it can do
> similar things like Perl modules. But C isn't object oriented. Inheritance is an
> essential characteristic of object orientation.

Funny thing about how languages "are" or "are not" object oriented....
back in the day, I used to do SOM programming on OS/2, which was
entirely in C, and most definitely had all the features you'd expect
of object orientation. Inheritance, the ability to override your
parent's methods, the ability to call the parent's method during the
implementation of your own, etc, etc, etc. It was all done through
DLLs, so you could subclass someone else's class (a very common thing
was to subclass one of the standard classes and add a small amount of
functionality to it) without having the source code. So, yeah, I don't
think there's any fundamental in any language that says whether or not
you can do OOP.

There are features which make OOP easier, and some languages have more
of them than others do. That's about all you can really say.

ChrisA

Antoon Pardon

unread,
Oct 19, 2020, 4:27:07 AM10/19/20
to


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.

Stephen Tucker

unread,
Oct 19, 2020, 5:08:16 AM10/19/20
to
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>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Grant Edwards

unread,
Oct 19, 2020, 1:13:27 PM10/19/20
to
On 2020-10-19, Stephen Tucker <stephen...@sil.org> wrote:

> 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.)

That's the first thing I thought of, though I would probably use a
non-space character to avoid convusion when reading:

mystr = r'abc\_'[:-1]

--
Grant

Eryk Sun

unread,
Oct 19, 2020, 2:22:09 PM10/19/20
to
But it doesn't actually "end a raw string with a single backslash".
The compiler could be optimized for slicing string literals, but it's
not. For example:

>>> dis.dis(r"r'spam\eggs\_'[:-1]")
1 0 LOAD_CONST 0 ('spam\\eggs\\_')
2 LOAD_CONST 1 (None)
4 LOAD_CONST 2 (-1)
6 BUILD_SLICE 2
8 BINARY_SUBSCR
10 RETURN_VALUE

For comparison:

>>> dis.dis(r"r'spam\eggs' '\\'")
1 0 LOAD_CONST 0 ('spam\\eggs\\')
2 RETURN_VALUE

Peter J. Holzer

unread,
Apr 24, 2021, 9:32:13 AM4/24/21
to
On 2020-10-19 06:24:18 -0000, Mladen Gogala via Python-list wrote:
> On Mon, 19 Oct 2020 02:44:25 +0000, Stefan Ram wrote:
> > Mladen Gogala <mgo...@yahoo.com> writes:
> >>In Perl, there are no classes.
> >
> > If there are no classes in Perl, then what does
> >
> > bless REF,CLASSNAME
> >
> > do?
>
> bless \$ref will make the given reference a reference to the class. And classes is Perl
> are called "modules". However, Perl classes are not the classes in the real sense. There
> is no inheritance.

That's wrong. Perl classes have inheritance, and they always have had it
since they were introduced in Perl 5.0 (in 1994).


> You can have a similar thing in C: it's called "struct", it can do
> similar things like Perl modules.

Nope. C structs are data structures. Perl modules are primarily a way to
structure code.


> But C isn't object oriented. Inheritance is an essential
> characteristic of object orientation.

You can have that even in C. One of my first major C programs (back in
the 1980s) was object-oriented (including inheritance). I didn't even
know what "object oriented" meant back then, but the library we used for
the GUI stuff used that style so it felt natural to extend it to
"application level" objects.

> There were attempts to turn Perl into OO language with Moose
> (https://metacpan.org/pod/Moose)

Moose just adds syntactic sugar. It may be easier to read (especially
for people used to other OO languages) and save a bit of typing, but it
doesn't add anything you couldn't do in plain Perl5.
signature.asc
0 new messages