r"<path>"

Lie Ryan lie.1296 at gmail.com
Tue Sep 30 20:39:12 CEST 2008


On Tue, 30 Sep 2008 10:50:01 -0700, Kyle Hayes wrote:

>> Please describe the actual problem you're trying to solve. In what way
>> do slashes need to be "fixed," and why?
> 
> Well, I have decided to build a tool to help us sync files in UNC paths.
> I am just building the modules and classes right now so I haven't
> developed the frontend yet. I am assuming when the user provides a path
> (either by typing it in, or pulling it in from a config file), the UNC
> slashes are going to escape stuff in the string, so I want to double
> them up.
> 
> I understand if the best way is to convert all the slashes to double-
> slashes. But the 'r' function seemed so handy and convenient.

You don't need to. Python's string is never escaped in-memory, it is only 
escaped when repr(s) is called (the interpreter's implicit print uses repr
() instead of str()). And that means all string coming and going to/from 
IO (file, GUI, etc) is stored as-is. However, strings that comes from 
source code or interpreter prompt (a.k.a. literal string) needs to be 
escaped.

Analogy: When you're writing a string in the source code, you add double 
quotes (""), right? But do you think the quotes are stored in memory? No, 
it's just an escape character to differentiate a string from its 
surrounding.




More information about the Python-list mailing list