liveBook · Manning

Raw string literal

A raw string literal is a string-literal syntax that treats most backslashes and other contents literally, reducing ordinary escape processing.

How 3 Manning books explain it

The Quick Python Book, Fourth Edition

A raw string literal is a Python string literal in which backslashes are generally treated as literal characters rather than introducing escape sequences.

How it is used
Uses raw strings to preserve backslashes in regular expressions and generated Python code.
How deep it goes
Explains prefix and quoting forms, compares string values, and traces Python and regex escaping separately.
Examples
Compares tab escapes, compiles a literal-backslash pattern, and writes a module containing a newline escape.
What you can do after
Apply raw string literals with re.compile and when writing Python code to a file.

Read how The Quick Python Book, Fourth Edition explains raw string literal →

Natural Language Processing in Action, Second Edition

A Python string literal in which backslashes are preserved rather than normally interpreted as escape sequences.

How it is used
Simplifies writing regular expressions by preserving backslashes for the regular-expression parser.
How deep it goes
Defines backslash preservation and contrasts raw patterns with doubled escapes, without detailing string-parser internals.
Examples
Uses whitespace matching, literal curly braces, greeting patterns, and compiled regular expressions as examples.
What you can do after
Apply raw string literals to write more readable Python regular-expression patterns with unmodified backslashes.

Read how Natural Language Processing in Action, Second Edition explains raw string literal →

100 C++ Mistakes and How to Avoid Them

A raw string is a C++ string literal whose contents are written without interpreting ordinary escape sequences.

How it is used
Clarifies regular-expression patterns by separating their characters from C++ string-escape syntax.
How deep it goes
Defines the opening and closing delimiters and demonstrates their effect through a pattern comparison.
Examples
Compares ordinary and raw string literals for a pattern matching two backslashes and uses a regular expression for email validation.
What you can do after
Understand how raw strings make regular-expression patterns easier to read without C++ escape notation.

Read how 100 C++ Mistakes and How to Avoid Them explains raw string literal →

Also covered in

What to know first

Related concepts

Questions about raw string literal

What is raw string literal?

A raw string literal is a string-literal syntax that treats most backslashes and other contents literally, reducing ordinary escape processing.

Which Manning books explain raw string literal?

The Quick Python Book, Fourth Edition, Natural Language Processing in Action, Second Edition and 100 C++ Mistakes and How to Avoid Them.

What should I know before learning raw string literal?

Manning books that teach raw string literal usually introduce string literal, escape sequence, regular expression and backslash first.

What is the difference between raw string literal and escape sequence?

A raw string literal is a string-literal syntax that treats most backslashes and other contents literally, reducing ordinary escape processing. An escape sequence is source notation using a marker to change how following characters are interpreted, often representing a special character or control value or making syntax literal.

What is raw string literal a kind of?

Raw string literal is a kind of string literal. A string literal is a fixed sequence of text characters written directly in a programming or data-language expression as a string value, rather than computed from other data.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.