Rawest raw string literals

Mikhail V mikhailwas at gmail.com
Sat Apr 22 17:13:26 EDT 2017


On 20 April 2017 at 18:40, Grant Edwards <grant.b.edwards at gmail.com> wrote:
> On 2017-04-20, Mikhail V <mikhailwas at gmail.com> wrote:
>> On 20 April 2017 at 17:59, Grant Edwards <grant.b.edwards at gmail.com> wrote:
>>> On 2017-04-20, Mikhail V <mikhailwas at gmail.com> wrote:
>>>> Quite often I need raw string literals for concatenating console commands.
>>>> I want to input them exactly as they are in python sources.
>>>>
>>>> There is r"" string, but it is obviously not enough because e.g. this:
>>>> s = r"ffmpeg -i  "\\server-01\D\SER_Bigl.mpg" "
>>>
>>>    s = r'ffmpeg -i  "\\server-01\D\SER_Bigl.mpg" '
>>>
>>> Does that do what you want?
>>
>> Yes but it still needs to watch out if there is no ' inside or vice
>> versa with " characters if use r"". I would like a universal
>> solution.
>
> IOW, you want something that just reads your mind.
>
> How can there exist a "universal solution" even in theory?
>
> There has to be some sort of "end of literal" terminator character
> sequence.  That means there has to be some sort of escaping mechanism
> when that "end of literal" sequence appears in the literal itself.
>

In *theory* one can define what characters can be part of the raw string.
In Python e.g. the newline character cannot be part of
the r"" string (unless a line ends with \):

k = r"abc
      def"

gives an error:

    k = r"abc
            ^
SyntaxError: EOL while scanning string literal

So one could define a rule, that a raw string *must*
be terminated by the sequence quote + newline.
In theory.


Mikhail


More information about the Python-list mailing list