r"<path>"

K

Kyle Hayes

Is there a way to use the 'r' in front of a variable instead of
directly in front of a string? Or do I need to use a function to get
all of the slashes automatically fixed?

/thanks
-Kyle
 
C

Carsten Haese

Kyle said:
Is there a way to use the 'r' in front of a variable instead of
directly in front of a string? Or do I need to use a function to get
all of the slashes automatically fixed?

Please describe the actual problem you're trying to solve. In what way
do slashes need to be "fixed," and why?
 
K

Kyle Hayes

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.

I am very new to Python, but not at all to programming.

Thanks!
 
M

Mark Thomas

Is there a way to use the 'r' in front of a variable instead of
directly in front of a string? Or do I need to use a function to get
all of the slashes automatically fixed?

Is this what you're talking about?

str = "foo/bar"
re = Regexp.new(str) => /foo\/bar/

-- Mark.
 
C

Carsten Haese

Kyle said:
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.

That assumption is incorrect. While backslashes in string literals are
escape characters that must be doubled up to convey literal backslashes,
no such interpretation is made for backslashes that are read from a GUI
text box or from a file.

See for yourself:blah\blah\blah

Carry on and come back when you actually have a problem ;)
 
L

Lie Ryan

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top