String Formatting
A string is a sequence of characters, ordered by position, and is one of the most frequently used data types in Python.
Text as Ordered Characters
When a Python program contains a name, message, sentence, digit, space, punctuation mark, or other typed symbol, that text can be represented as a string. A string is a sequence of characters arranged in order. It is not simply an unstructured blob of text: each character occupies a position in the sequence.
A string is a sequence of characters, ordered by position.
Positions begin at index 0, which is the leftmost character. In the diagram, the string code has c at position 0, o at position 1, d at position 2, and e at position 3. The important idea is that the characters remain ordered whether you consider the complete string or an individual character.
Quoting a Single-Line Value
Python recognizes text as a string when the text is enclosed by matching quote marks. Single quotes and double quotes are functionally equivalent: both can mark the beginning and end of a string. The choice often depends on which quote characters already appear inside the text.
Matching the Boundary to the Content
Choose a suitable quoting method for a question containing an apostrophe.
Inspect the content: The question contains the apostrophe in the contraction What's.
Choose the boundary: Double quotes can surround the question without treating the apostrophe as the closing boundary.
Form the string: The resulting string uses double quotes around the full question.
"What's your name?"
Preserving Whitespace
Whitespace inside the quotes belongs to the string. Python preserves spaces, tabs, and line breaks exactly as they are written. It does not trim or normalize that whitespace inside a string.
Reading the Whitespace Literally
Compare the strings 'Quote me on this' and 'Quote me on this'.
Compare the visible words: Both strings contain the same words in the same order.
Compare the spaces: The second string has two spaces between its words, while the first has one.
Apply the preservation rule: Those additional spaces are characters within the second string and are preserved.
The two strings are different because their whitespace is different.
Choosing Delimiters
| Quoting method | Best fit | Important behavior |
|---|---|---|
| Single quotes | Text containing quotation marks | Functionally equivalent to double quotes |
| Double quotes | Text containing apostrophes or contractions | Functionally equivalent to single quotes |
| Triple quotes | Text spanning multiple lines | Can contain both single and double quotes freely |
Triple quotes can be written as three single quotes in a row or three double quotes in a row. They allow a string to span multiple lines, and every line break between the opening and closing triple quotes becomes part of the string. They also allow single quotes and double quotes to appear naturally within the text.
Selecting a Method for Three Kinds of Content
Select a quoting method for an apostrophe, quotation marks, and multi-line text.
Apostrophe: Use double quotes for text such as What's your name? so the apostrophe does not act as the boundary.
Quotation marks: Use single quotes when the text contains quotation marks, because the inner double quotes do not conflict with the outer single quotes.
Multiple lines: Use triple quotes when the string must include line breaks.
The most suitable method depends on the characters and line structure inside the string.
Projects may also adopt a consistent style, such as preferring single quotes or double quotes for all strings. When a team or project has a convention, follow it unless the string content makes another choice clearer.
Mistakes Beginners Make
Using single quotes around text that contains an apostrophe without considering the boundary.
The apostrophe in What's can be interpreted as the closing quote, leaving the remaining text outside the string.
Fix:
Use double quotes around this text.Assuming Python removes extra spaces inside a string.
Whitespace inside quotes is preserved exactly as typed.
Fix:
Treat every space, tab, and line break inside the quotes as part of the string.Using ordinary single-line quotes for text that must contain line breaks.
Triple quotes are the quoting method that enables multi-line strings.
Fix:
Use three single quotes or three double quotes around multi-line text.Thinking single quotes and double quotes create fundamentally different kinds of strings.
They are functionally equivalent; their practical difference is how conveniently they fit the string's content.
Fix:
Choose the style that avoids conflicts with apostrophes or quotation marks, while following project conventions.
Practice Check
For each description, choose single quotes, double quotes, or triple quotes. Then explain why: a string containing an apostrophe, a string containing quotation marks, and a string containing multiple lines.
Hints
- Single and double quotes are functionally equivalent, so inspect the characters already inside the text.
- Remember that triple quotes can include line breaks.
- Check whether the selected outer quote could be mistaken for a character inside the string.
- A string is an ordered sequence of characters. Enclose single-line text in matching single or double quotes, choosing the style that fits the text's apostrophes or quotation marks. Whitespace inside quotes is preserved exactly. Use triple quotes for multi-line strings or when the text needs to contain both single and double quotes freely.
Key Takeaways
- A string is a sequence of characters ordered by position, beginning at index 0.
- Single quotes and double quotes create functionally equivalent strings.
- Spaces, tabs, and line breaks inside quotes are preserved exactly.
- Triple quotes support multi-line strings and allow both single and double quotes inside the text.
- Choose a quoting method based on the string content and any project style convention.