Raw Strings

A feature of PythonLanguage. A rawstring is a string literal (prefixed with an r) in which the normal escaping rules have been suspended so that everything is a literal.

For example:

print r"//*/*//**" actually prints "//*/*//**"

This is incredibly useful for writing readable regular expressions or any string literals that will be processed by other escaping systems such as SQL parsers.

Everything? Even the quote character?

No. The end of the string literal is determined as for a non-raw string literal. So \" does not end the literal, but produces two characters. It's not possible for a r" ... " string to contain a " not preceded by a \, and a raw string cannot end with an odd number of \s.


Ditto CeeSharp:

  string s = @"C:\Wiki\Suffers\Fools\Gladly";
How do I display the string "C:\Wiki\Suffers\Fools\Gladly"; (with the semi-colon)? I assume that some escaping rules still apply? Ditto for Python, how would I show the new line in the String? (If the answer is "you can't, so go back to using normal strings", that's okay)

A semi-colon inside a string (literal or not) doesn't need escaping. The only character that is parsed within a regular string is the backslash, which must be followed by an escapable character (n, r, t etc.).

What about the quote character?

Currently, to print the string (including the quote character)

  C:\Wiki\Suffers\Fools\Gladly";
in C, Java, and related languages, one must say something like
  string s = "C:\\Wiki\\Suffers\\Fools\\Gladly\"";
In C#, you may still use the @ token to remove the need for all escaping between the quotes. However, to include a quote character in a string, you must double-quote it:
  string s = @"C:\Wiki\Suffers\Fools\Gladly"""; // C:\Wiki\Suffers\Fools\Gladly"


How do I display the string "C:\Wiki\Suffers\Fools\Gladly"; (with the semi-colon)?

In RubyLanguage, create a "single-quoted" string with a pair of delimiters that don't appear in the string:

 %q["C:\Wiki\Suffers\Fools\Gladly";]
There I used square brackets, but probably any punctuation but backslashes will do:
 %q*"C:\Wiki\Suffers\Fools\Gladly";*


It's a piece of cake in FortranLanguage 77, where all strings are raw anyway:

 '"C:\Wiki\Suffers\Fools\Gladly";'
Strings are delimited by single quotes. To put a single quote within a string, just insert two single quotes:
 'It''s a piece of cake.'
Well, okay, so maybe I had to "escape" that single quote, but at least Fortrash doesn't worry about backslashes and double quotes.


In CeePlusPlusEleven, raw strings begin R"( and end )":

  R"(C:\Wiki\Suffers\Fools\Gladly";)"
If your string contains a literal )" you can make the delimiters longer by adding characters between the parens and double quotes:
  R"blah(C++11 raw strings look like:

const char *str = R"(foo\bar)"; )blah"


All this "escaping" business makes writing a QuineProgram a bit tricky.


CategoryCodingIssues ?


EditText of this page (last edited April 2, 2014) or FindPage with title or text search