By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,140 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,140 IT Pros & Developers. It's quick & easy.

r"<path>"

P: n/a
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
Sep 30 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Kyle Hayes wrote:
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?

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 30 '08 #2

P: n/a
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!
Sep 30 '08 #3

P: n/a
On Sep 30, 1:17*pm, Kyle Hayes <mrkyleha...@gmail.comwrote:
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.
Sep 30 '08 #4

P: n/a
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.
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:
>>some_string = raw_input("Enter a string: ")
Enter a string: blah\blah\blah
>>print some_string
blah\blah\blah

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

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 30 '08 #5

P: n/a
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.

Sep 30 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.