Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python raw literal string

str = r'c:\path\to\folder\'   # my comment
  • IDE: Eclipse
  • Python2.6

When the last character in the string is a backslash, it seems like it will escape the last single quote and treat my comment as part of the string. But the raw string is supposed to ignore all escape characters, right? What could be wrong? Thanks.

like image 959
Stan Avatar asked Aug 19 '10 00:08

Stan


People also ask

What is a raw string literal?

Raw string literals are string literals that are designed to make it easier to include nested characters like quotation marks and backslashes that normally have meanings as delimiters and escape sequence starts. They're useful for, say, encoding text like HTML.

Can a string be a literal in Python?

A string literal can be created by writing a text(a group of Characters ) surrounded by a single(”), double(“”), or triple quotes. By using triple quotes we can write multi-line strings or display them in the desired way. Example: Here geekforgeeks is a string literal that is assigned to a variable(s).

What is the difference between string and raw string in Python?

Unlike a regular string, a raw string treats the backslashes ( \ ) as literal characters. Raw strings are useful when you deal with strings that have many backslashes, for example, regular expressions or directory paths on Windows.

How do I convert a string to a raw string in Python?

Use the built-in function repr() to convert normal strings into raw strings. The string returned by repr() has ' at the beginning and the end. Using slices, you can get the string equivalent to the raw string.


2 Answers

Raw string literals don't treat backslashes as initiating escape sequences except when the immediately-following character is the quote-character that is delimiting the literal, in which case the backslash does escape it.

The design motivation is that raw string literals really exist only for the convenience of entering regular expression patterns – that is all, no other design objective exists for such literals. And RE patterns never need to end with a backslash, but they might need to include all kinds of quote characters, whence the rule.

Many people do try to use raw string literals to enable them to enter Windows paths the way they're used to (with backslashes) – but as you've noticed this use breaks down when you do need a path to end with a backslash. Usually, the simplest solution is to use forward slashes, which Microsoft's C runtime and all version of Python support as totally equivalent in paths:

s = 'c:/path/to/folder/'

(side note: don't shadow builtin names, like str, with your own identifiers – it's a horrible practice, without any upside, and unless you get into the habit of avoiding that horrible practice one day you'll find yourseld with a nasty-to-debug problem, when some part of your code tramples over a builtin name and another part needs to use the builtin name in its real meaning).

like image 185
Alex Martelli Avatar answered Sep 30 '22 06:09

Alex Martelli


It's IMHO an inconsistency in Python, but it's described in the documentation. Go to the second last paragraph:

http://docs.python.org/reference/lexical_analysis.html#string-literals

r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes)

like image 23
Eike Avatar answered Sep 30 '22 05:09

Eike