Working with Strings
A string represents text, while a value such as 5 or 1.23 is a number.
Recognizing Text Values
Strings are values made from characters. In practical terms, a string is a sequence of characters, or what the source describes as a bunch of words. You will use strings in almost every Python program, so recognizing them is an important first step.
A string represents text, while a value such as 5 or 1.23 is a number.
Literals and Their Contents
A literal constant is a value written directly. The source gives numbers such as 5 and 1.23 as examples of literal constants, and it also gives text such as 'This is a string' and "It's a string!" as examples. The quotation marks show that the value is a string; the characters between them make up the string's content.
| Written value | What it illustrates |
|---|---|
| 5 | A numeric literal |
| 1.23 | A numeric literal |
| 'This is a string' | A string literal |
| "It's a string!" | A string literal |
Examples of literal constants from the source
String: a sequence of characters specified as text, such as 'This is a string'.
Reading the Delimiters
To specify a string, place its characters inside quotation marks. The source specifically shows single quotes in 'Quote me on this'. In that example, the first single quote marks where the string begins, and the final single quote marks where it ends. The words between those marks are the string's characters.
Worked Recognition
Classifying four literal values
Decide which examples are numeric literals and which are string literals.
- 1First value
5 is a number written directly, so it is a numeric literal.
- 2Second value
1.23 is a number written directly, so it is a numeric literal.
- 3Third value
'This is a string' is enclosed in quotation marks, so it is a string literal. Its content is the sequence of characters This is a string.
- 4Fourth value
"It's a string!" is enclosed in quotation marks, so it is a string literal. Its content is It's a string!.
The first two examples are numeric literals. The last two examples are string literals.
What do you think happens?
Which part is the string's content in 'Quote me on this'?
Reveal answer
Quote me on this
The quotation marks specify the string's boundaries. The characters between them are the string's content.
Mistakes with String Boundaries
Common mistake Treating a number such as 5 as a string simply because it is written as a literal.
What it looks like5 and '5' are read as different kinds of written values.
Why it failsThe source identifies 5 as a numeric literal and identifies quoted text as a string literal.
Do this insteadCheck whether the value is a number written directly or text enclosed in quotation marks.
Common mistake Including the quotation marks as part of the string's content.
What it looks likeIn 'Quote me on this', the content is Quote me on this.
Why it failsThe quotation marks specify the beginning and end of the string.
Do this insteadRead the characters between the boundary marks as the string's content.
Common mistake Failing to notice that a string can contain several words.
What it looks like'This is a string'
Why it failsA string is a sequence of characters and can be a bunch of words.
Do this insteadTreat the complete sequence between the quotation marks as one string value.
Practice Check
Classify each value as a numeric literal or a string literal: 5, 1.23, 'Quote me on this', and "It's a string!". For each string, state the characters that make up its content.
Hints
- Look for quotation marks.
- The content of a string is the sequence of characters between its boundary marks.
Essential Takeaways
- 1A string is a sequence of characters.
- 2Strings are commonly used to represent words and other text.
- 3Quotation marks specify the beginning and end of a string.
- 4The characters between the quotation marks are the string's content.
- 5Values such as 5 and 1.23 are numeric literals, while quoted text is a string literal.
At a glance
- A string is a sequence of characters used for text.
- A literal constant is a value written directly, such as 5, 1.23, or quoted text.
- Quotation marks define a string's boundaries.
- The characters inside those boundaries form the string's content.