escaping

Terry Reedy tjreedy at udel.edu
Sun Apr 15 18:42:27 EDT 2012


On 4/15/2012 5:07 PM, Kiuhnm wrote:
> This is the behavior I need:
> path = path.replace('\\', '\\\\')
> Is there a better way?

For one-time use, and given that you cannot un-double with the r prefix, 
not that I know of. For using the substrings multiple times, name them.

 >>> s = r'abc\cd\ef'
 >>> bs = '\\'
 >>> bs2 = bs+bs
 >>> s.replace(bs, bs2)
'abc\\\\cd\\\\ef'
 >>> len(s.replace(bs, bs2))
11
# and continue to use bs and bs2

-- 
Terry Jan Reedy




More information about the Python-list mailing list