String Literals and Escape Sequences
An example of a literal constant is a number like 5 , 1.23 , or a string like 'This is a string' or "It's a string!" .
A String Begins as Literal Text
A literal constant is a value written directly in a program. The source material gives numbers such as 5 and 1.23 as examples, and it also gives strings such as 'This is a string' and "It's a string!". A string literal uses quotation marks to show which text belongs to the string.
The opening quotation mark tells the language that the string starts there. The closing quotation mark tells it that the string ends there. The text between them is the string content. The source examples use either single quotation marks or double quotation marks.
Choosing Quotation Marks
Representing an Apostrophe
How can the string What's your name? be specified?
Use double quotation marks: The source gives "What's your name?" as one way to specify this string. The apostrophe is then part of the string content rather than the mark that ends the string.
Use an escape sequence when needed: If a double quote itself must appear inside a double-quoted string, the double quote must be represented with an escape sequence.
Quotation-mark choice affects how quotation marks inside the text are represented.
A practical way to represent text containing an apostrophe is to use double quotation marks, as in "What's your name?". The source also explains the reverse situation: a double quote inside a double-quoted string requires an escape sequence. The quotation mark that surrounds the string and the quotation mark intended as content must be distinguished.
Reading an Escape Sequence
An escape sequence is a way to represent a special character or special handling inside a string literal. The source specifically identifies \n as the escape sequence for the newline character. It can indicate that the next part of the string starts on a new line.
In the source text A\nB, the two characters backslash and n are used together as the newline escape sequence. When the string is interpreted, the content is arranged as A on one line and B on the next line. The sequence therefore represents a newline rather than ordinary visible text consisting of a backslash followed by n.
Raw Strings and File Paths
Sometimes a string should contain text without special processing such as escape-sequence handling. For that purpose, the source describes a raw string: prefix the string with r or R. A raw string can be used instead of writing double-backslash escape sequences in situations such as a Windows-style path.
| Form | Meaning described by the source |
|---|---|
| C:\\Documents | Uses a double-backslash escape sequence to represent the backslash. |
| r'C:\Documents' | Uses a raw string so the backslash does not receive special escape-sequence processing. |
| C:\Documents | Should not be used in the source example because \D becomes an unknown escape sequence. |
Mistakes with String Boundaries
Treating an apostrophe inside a string as though it must always be escaped.
The apostrophe can conflict with the single quotation marks that surround the string.
Fix:
Use the double-quoted form "What's your name?", as shown in the source.Putting a double quote inside a double-quoted string without using an escape sequence.
The language may interpret that double quote as the end of the string.
Fix:
Use an escape sequence for the double quote itself.Writing a backslash as ordinary text when the backslash is intended to appear in the string.
The backslash can begin an escape sequence, such as the unknown escape sequence \D described in the source.
Fix:
Use a double-backslash escape sequence or prefix the string with r or R.Expecting \n to remain ordinary visible backslash-and-n text.
The source defines \n as the newline character escape sequence.
Fix:
Read \n as the point where the string begins a new line.
Check Your Interpretation
For each item, decide whether the quotation marks are being used to delimit a string, whether an escape sequence is needed, or whether a raw string would be appropriate: 5, 'This is a string', "It's a string!", A\nB, and a Windows-style path containing backslashes.
Hints
- A number such as 5 is a literal constant, but it is not a string example in the source.
- Look at which quotation mark surrounds each string and whether the same kind of quotation mark appears inside it.
- For A\nB, identify the special meaning assigned to \n.
- For a path containing backslashes, compare a double-backslash escape sequence with an r- or R-prefixed raw string.
Classifying the Source Examples
What role does each source example demonstrate?
5 and 1.23: These are examples of numeric literal constants.
'This is a string': This is an example of a string literal surrounded by single quotation marks.
"It's a string!": This is an example of a string literal surrounded by double quotation marks while containing an apostrophe.
A\nB: The sequence \n indicates a newline character and separates the text into two lines.
A path with backslashes: The backslashes should be represented with double-backslash escape sequences or handled with a raw string, according to the source.
String literals are delimited by quotation marks, while escape sequences control how special characters such as quotation marks, backslashes, and newlines are represented.
Key Takeaways
- A string literal is a literal constant written between quotation marks.
- Single and double quotation marks can be chosen to make quotation marks inside the text easier to represent.
- Escape sequences represent special characters or special handling, including quotation marks, backslashes, and the newline character \n.
- A raw string prefixed with r or R avoids special processing such as escape-sequence handling.
- For paths containing backslashes, use double-backslash escape sequences or a raw string rather than leaving a backslash to begin an unknown escape sequence.
Key Takeaways
- String literals are literal constants surrounded by quotation marks.
- Quotation-mark choice matters when the string content includes an apostrophe or a double quote.
- The escape sequence \n represents a newline, while \\ represents a backslash.
- Raw strings use an r or R prefix when escape-sequence processing is not wanted.
- Backslashes in paths must be handled with an escape sequence or a raw string.