python raw strings?
Leif K-Brooks
eurleif at ecritters.biz
Wed Aug 25 02:45:52 EDT 2004
Maurice LING wrote:
> What are python raw strings and how are they different from regular
> strings?
First of all, a minor semantic correction. A string is not raw or
non-raw; a string can be expressed using raw syntax, but it's just a
different way of writing the same thing.
Now, in Python, strings can be written with special sequences involving
backslashes; for instance, "\n" means a line break instead of a
backslash and an n. If you want a literal backslash followed by an n,
you need to use two backslashes: "\\n".
If you need to use a lot of backslashes, doubling them up can get very
annoying. The raw string syntax fixes that: r"\n" is a backslash
followed by an n, not a line break.
> In PLY, "+" is "r'\+'" but "-" is "r'-'", why is there an extra "\" in
> "+"? When to use the extra "\" and when not?
I don't have any experience with PLY, but I'm guessing it uses regular
expressions (http://python.org/doc/current/lib/re-syntax.html). In a
regular expression, + means something special; if you don't want the
special meaning, you need to put a backslash in front of it.
The minus sign, on the other hand, has no special meaning in regular
expressions (well, outside of character sets), so it can be used on its
own with no backslash.
More information about the Python-list
mailing list