Rawest raw string literals

Chris Angelico rosuav at gmail.com
Thu Apr 20 12:33:56 EDT 2017

On Fri, Apr 21, 2017 at 2:16 AM, 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.

There's no universal solution, by definition. The nearest you'll get
is something like the PostgreSQL "dollar string":

$SomeTag$any text$SomeTag$

You can choose any tag you like for SomeTag and it'll match only the
corresponding tag, so this can be nested. But it's long and ugly,
especially when you don't need to actually nest it.

Python is not a shell scripting language, and trying to treat it as
one is usually going to get you into tangles like this. Much better,
IMO, is to treat Python as a scripting language, where higher-level
constructs like lists are the norm.


More information about the Python-list mailing list