19

I found this potentially very useful python script, but came across these expressions which I've never seen before:

inputfilename = r'/path/to/infile'
outputfilename = r'/path/to/outfile'

I can't find a way to search for it. what does the r'...' do?

Thanks for your help!

Lisa
  • 3,365
  • 3
  • 19
  • 30
  • 2
    This is a "raw string," see [the documentation](http://docs.python.org/3.3/library/re.html#raw-string-notation). Often used to be able to write special characters (e.g. `\`) without escaping. – bbayles Sep 26 '13 at 17:34
  • 1
    r is a raw string literal which enables you to forget about the escape sequences! http://stackoverflow.com/questions/3517802/python-raw-literal-string – Vivek Sep 26 '13 at 17:35
  • It doesn't make a difference in this case, but if the path had backslashes you would want to include the `r` – beroe Sep 26 '13 at 17:38
  • Thanks everyone! And yes, the posted link also answers my question. – Lisa Sep 26 '13 at 17:43

3 Answers3

16

The r'..' string modifier causes the '..' string to be interpreted literally. That means, r'My\Path\Without\Escaping' will evaluate to 'My\Path\Without\Escaping' - without causing the backslash to escape characters. The prior is equivalent to 'My\\Path\\Without\\Escaping' string, but without the raw modifier.

Note: The string cannot end with an odd number of backslashes, i.e r'Bad\String\Example\' is not a correct string.

Maciej Gol
  • 15,394
  • 4
  • 33
  • 51
  • 4
    ...with one exception: `r'My\Path\Without\Escaping\'` doesn't work. Even raw strings may not end in an odd number of backslashes. – Tim Pietzcker Sep 26 '13 at 17:52
  • @MaciejGol - So the folder depth needs to be an even number for this to work!? – DavidR Oct 20 '18 at 19:08
  • @DavidR, it has nothing to do with the folder depth. It's all about whether being forced to use double slashes `\\ ` without `r` modifier, or single slashes `\\ ` with `r` modifier. – Maciej Gol Oct 21 '18 at 20:07
3

It's called raw string literal.

According to Python String literals documentation:

The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences.

...

Unless an 'r' or 'R' prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C

falsetru
  • 357,413
  • 63
  • 732
  • 636
2

It just means raw string in Python. Meaning that whatever is inside the string, is the string. For example, if you wanted to add slashes:

string1 = "happy\/cheese"

You would need to add \ in front of the slash since its an escaping character. For example, \n means a new line.

If you keep the string raw, it makes sure that whatever is in the string is not interpreted a special way. for example if you wrote string2 = r"\n", it would just give you "\n" as you string, and not a new line.

You can learn more about them, from here.

Now, this is done in your case, because file paths have a lot of slashes, and we'd like to avoid having to put in so many backslashes.

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199