Correct syntax for pathological re.search()

Jon Ribbens jon+usenet at unequivocal.eu
Mon Oct 7 11:43:59 EDT 2024


On 2024-10-07, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
> "Michael F. Stemper" <michael.stemper at gmail.com> wrote or quoted:
>>For now, I'll use the "r" in a cargo-cult fashion, until I decide which
>>syntax I prefer. (Is there any reason that one or the other is preferable?)
>
>   I'd totally go with the r-style notation!
>
>   It's got one bummer though - you can't end such a string literal with
>   a backslash. But hey, no biggie, you could use one of those notations:
>
>   main.py
>
> path = r'C:\Windows\example' + '\\'
>
> print( path )
>     
> path = r'''
> C:\Windows\example\
> '''.strip()
>
> print( path )
>
>   stdout
>
> C:\Windows\example\
> C:\Windows\example\
>
>   .

... although of course in this example you should probably do neither of
those things, and instead do:

    from pathlib import Path
    path = Path(r'C:\Windows\example')

since in a Path the trailing '\' or '/' is unnecessary. Which leaves
very few remaining uses for a raw-string with a trailing '\'...


More information about the Python-list mailing list