Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Literal r'\' Not Accepted

Tags:

r'\' in Python does not work as expected. Instead of returning a string with one character (a backslash) in it, it raises a SyntaxError. r"\" does the same.

This is rather cumbersome if you have a list of Windows paths like these:

paths = [ r'\bla\foo\bar',           r'\bla\foo\bloh',           r'\buff',           r'\',           # ...         ] 

Is there a good reason why this literal is not accepted?

like image 874
Alfe Avatar asked Apr 03 '12 12:04

Alfe


People also ask

What does \r mean in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

What does \r mean string?

The r means that the string is to be treated as a raw string, which means all escape codes will be ignored. For an example: '\n' will be treated as a newline character, while r'\n' will be treated as the characters \ followed by n .

How do you use R string in Python?

Python raw string is created by prefixing a string literal with 'r' or 'R'. Python raw string treats backslash (\) as a literal character. This is useful when we want to have a string that contains backslash and don't want it to be treated as an escape character.

How do you escape a raw string in Python?

Escape sequences In Python, characters that cannot be represented in a normal string (such as tabs, line feeds. etc.) are described using an escape sequence with a backslash \ (such as \t or \n ), similar to the C language.


2 Answers

This is in accordance with the documentation:

When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.

Use "\\" instead, or, better even, use / as path separator (yes, this works on Windows).

like image 88
Sven Marnach Avatar answered Sep 28 '22 09:09

Sven Marnach


The backslash can be used to make a following quote not terminate the string:

>>> r'\'' "\\'" 

So r'foo\' or r'\' are unterminated literals.

Rationale

Because you specifically asked for the reasoning behind this design decision, relevant aspects could be the following (although this is all based on speculation, of course):

  • Simplifies lexing for the Python interpreter itself (all string literals have the same semantics: A closing quote not followed by an odd number of backslashes terminates the string)
  • Simplifies lexing for syntax highlighting engines (this is a strong argument because most programming languages don't have raw strings that are still enclosed in single or double quotes and lots of syntax highlighting engines are badly broken because they use inappropriate tools like regular expressions to do the lexing)

So yes, there are probably important reasons why this way was chosen, even if you don't agree with these because you think that your specific use case is more important. It is however not, for the following reasons:

  • You can just use normal string literals and escape the backslashes or read the strings from a raw file
  • backslashes in string literals are typically needed in one of these two cases:
    • you provide the string as input to another language interpreter which uses backslashes as a quoting character, like regular expressions. In this case you won't ever need a backslash at the end of a string
    • you are using \ as a path separator, which is usually not necessary because Python supports / as a path separator on Windows and because there's os.path.sep.

Solutions

You can use '\\' or "\\" instead:

>>> print("\\") \ 

Or if you're completely crazy, you can use raw string literal and combine them with normal literals just for the ending backslash or even use string slicing:

>>> r'C:\some\long\freakin\file\path''\\' 'C:\\some\\long\\freakin\\file\\path\\' >>> r'C:\some\long\freakin\file\path\ '[:-1] 'C:\\some\\long\\freakin\\file\\path\\' 

Or, in your particular case, you could just do:

paths = [ x.replace('/', '\\') for x in '''    /bla/foo/bar   /bla/foo/bloh   /buff   /  '''.strip().split()] 

Which would save you some typing when adding more paths, as an additional bonus.

like image 38
Niklas B. Avatar answered Sep 28 '22 07:09

Niklas B.