15

I have string variable which is

temp = '1\2\3\4'

I would like to add a prefix 'r' to the string variable and get

r'1\2\3\4'

so that I can split the string based on '\'. I tried the following:

r'temp'
'r' + temp
r + temp

But none of the above works. Is there a simple to do it? I'm using python 3. I also tried to encode the string, using

temp.encode('string-escape')

But it returns the following error

LookupError: unknown encoding: string-escape
6
  • 2
    How are the strings being created in the first place? If you are doing it in your code, just add the r yourself in the code? If you are getting the input from the user, then the user needs to input the correct string in the first place. Commented Dec 15, 2016 at 3:00
  • The string is actually a file location that is generated using glob.iglob () function. I would like to get all the txt file in a certain file, and I would like to extract the name of those files by splitting '\'. Commented Dec 15, 2016 at 3:18
  • @SethMMorton, see my comments above. Thanks for your reply. Commented Dec 15, 2016 at 3:19
  • 1
    The results of glob.iglob() should not suffer any escaping problem trying to do this post-escape isn't necessary. Commented Dec 15, 2016 at 3:20
  • @SethMMorton Ah... I see. Yeah, it works. Thanks! Commented Dec 15, 2016 at 19:41

3 Answers 3

9

r is a prefix for string literals. This means, r"1\2\3\4" will not interpret \ as an escape when creating the string value, but keep \ as an actual character in the string. Thus, r"1\2\3\4" will have seven characters.

You already have the string value: there is nothing to interpret. You cannot have the r prefix affect a variable, only a literal.

Your temp = "1\2\3\4" will interpret backslashes as escapes, create the string '1\x02\x03\x04' (a four-character string), then assign this string to the variable temp. There is no way to retroactively reinterpret the original literal.

EDIT: In view of the more recent comments, you do not seem to, in fact, have a string "1\2\3\4". If you have a valid path, you can split it using

path.split(r'\')

or

path.split('\\')

but you probably also don't need that; rather, you may want to split a path into directory and file name, which is best done by os.path functions.

Sign up to request clarification or add additional context in comments.

2 Comments

Then how can I split the string variable? I would like to have results like ['1', '2', '3', '4']. I know I can do with r'1\2\3\4', but not '1\2\3\4'
There is nothing to split. "1\2\3\4" does not contain the characters 2, 3 or 4.
4

Wouldn't it just be re.escape(temp)?

Take for example the use case of trying to generate a pattern on the fly involving word boundaries. Then you can do this

r'\b' + re.escape(temp) + r'\b'

Comments

-4

just to prefix r in variable in search, Please do this r+""+temp. e.g.-

import re
email_address = 'Please contact us at: [email protected]'
searchString = "([\w\.-]+)@([\w\.-]+)"
re.serach(r""+searchString, email_address)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.