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!" .
Try it: Escape Sequences in String Literals
How Python turns the text of a string literal into characters: the tokenizer first finds where the literal ends (a backslash protects the next character; an unescaped inner quote or a line break in '…' ends it or breaks it), then each escape — \n, \t, \\, \', \", \xhh, \uXXXX, \N{name}, octal — becomes ONE character, a backslash at the end of a line joins lines, unknown escapes keep their backslash, and a raw string keeps everything as typed; len(), repr() and print() then show the result.
How it works
- The tokenizer scans for the closing quote: a backslash always skips the next character, so \' does not end a '…' string; an unescaped copy of the opening quote ends it (anything after is read as code), and a real line break inside '…' or "…" is 'unterminated string literal'. Triple quotes may span lines.
- The body is then decoded left to right: \n newline, \t tab, \\ one backslash, \' and \" quotes, \r, \a, \b, \f, \v, \ooo octal, \xhh (exactly 2 hex digits), \uXXXX, \UXXXXXXXX and \N{name} — each is ONE character.
- A backslash followed by a line break disappears (the source lines are joined); a backslash before a character that is not an escape is kept, with a SyntaxWarning; a truncated \x, \u or \U escape is a SyntaxError that names its positions.
- An r prefix makes a raw string: backslashes are kept as typed (but a raw string still cannot end in an odd backslash).
- len(s) counts the stored characters; repr(s) — what the interpreter echoes — writes invisible characters back as escapes; print(s) shows the characters themselves (line breaks, tab stops).
Default run (16 steps): Python reads a string literal opened by a single quote ('). First the tokenizer looks for where it ends; then the characters between the quotes are decoded. … print(s) shows the characters themselves: each newline starts a new line.
Simplified: One assignment s = <literal> of up to 80 characters and 4 lines, typed from printable ASCII plus a few accented letters and symbols; no f-strings or bytes. The lab's own tokenizer and decoder read the text — nothing is executed. Its value, len, repr, warnings and SyntaxError messages match CPython 3.12 on 2,500+ literals; \N{...} knows only 13 character names, and when the text after an early closing quote is an operator, keyword, f-string or new line the lab stops without saying what Python does next. print() output of invisible characters is shown as [\x..].
Loading the simulation…
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?
- 1Use 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.
- 2Use 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. |
The source contrasts an escaped backslash, a raw string, and an unhandled backslash.
Mistakes with String Boundaries
Common mistake Treating an apostrophe inside a string as though it must always be escaped.
What it looks like'What's your name?'Why it failsThe apostrophe can conflict with the single quotation marks that surround the string.
Do this insteadUse the double-quoted form "What's your name?", as shown in the source.
Common mistake Putting a double quote inside a double-quoted string without using an escape sequence.
What it looks likeA double-quoted string containing an unhandled double quote.
Why it failsThe language may interpret that double quote as the end of the string.
Do this insteadUse an escape sequence for the double quote itself.
Common mistake Writing a backslash as ordinary text when the backslash is intended to appear in the string.
What it looks likeA path written with a single unhandled backslash.
Why it failsThe backslash can begin an escape sequence, such as the unknown escape sequence \D described in the source.
Do this insteadUse a double-backslash escape sequence or prefix the string with r or R.
Common mistake Expecting \n to remain ordinary visible backslash-and-n text.
What it looks likeA\nBWhy it failsThe source defines \n as the newline character escape sequence.
Do this insteadRead \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?
- 15 and 1.23
These are examples of numeric literal constants.
- 2'This is a string'
This is an example of a string literal surrounded by single quotation marks.
- 3"It's a string!"
This is an example of a string literal surrounded by double quotation marks while containing an apostrophe.
- 4A\nB
The sequence \n indicates a newline character and separates the text into two lines.
- 5A 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
- 1A string literal is a literal constant written between quotation marks.
- 2Single and double quotation marks can be chosen to make quotation marks inside the text easier to represent.
- 3Escape sequences represent special characters or special handling, including quotation marks, backslashes, and the newline character \n.
- 4A raw string prefixed with r or R avoids special processing such as escape-sequence handling.
- 5For paths containing backslashes, use double-backslash escape sequences or a raw string rather than leaving a backslash to begin an unknown escape sequence.
At a glance
- 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.