13

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
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
ycenycute
  • 688
  • 4
  • 10
  • 20
  • 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. – SethMMorton Dec 15 '16 at 03: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 '\'. – ycenycute Dec 15 '16 at 03:18
  • @SethMMorton, see my comments above. Thanks for your reply. – ycenycute Dec 15 '16 at 03:19
  • 1
    The results of `glob.iglob()` should not suffer any escaping problem trying to do this post-escape isn't necessary. – SethMMorton Dec 15 '16 at 03:20
  • @SethMMorton Ah... I see. Yeah, it works. Thanks! – ycenycute Dec 15 '16 at 19:41
  • Does this answer your question? [Convert regular Python string to raw string](https://stackoverflow.com/questions/4415259/convert-regular-python-string-to-raw-string) – Karl Knechtel Jul 19 '23 at 21:28

3 Answers3

8

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.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 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' – ycenycute Dec 15 '16 at 02:30
  • There is nothing to split. `"1\2\3\4"` does not contain the characters `2`, `3` or `4`. – Amadan Dec 15 '16 at 02:31
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'
demongolem
  • 9,474
  • 36
  • 90
  • 105
-4

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

import re
email_address = 'Please contact us at: support@datacamp.com'
searchString = "([\w\.-]+)@([\w\.-]+)"
re.serach(r""+searchString, email_address)
Uday Singh
  • 155
  • 1
  • 8