Concepts / Working with Strings

Working with Strings

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!" .

  • Programming

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 valueWhat it illustrates
5A numeric literal
1.23A 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.

python

Worked Recognition

Classifying four literal values

Decide which examples are numeric literals and which are string literals.

First value: 5 is a number written directly, so it is a numeric literal.

Second value: 1.23 is a number written directly, so it is a numeric literal.

Third 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.

Fourth 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'?

  • The quotation marks only
  • Quote me on this
  • The entire written expression, including the quotation marks
Reveal answer

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

  • Treating a number such as 5 as a string simply because it is written as a literal.

    The source identifies 5 as a numeric literal and identifies quoted text as a string literal.

    Fix: Check whether the value is a number written directly or text enclosed in quotation marks.

  • Including the quotation marks as part of the string's content.

    The quotation marks specify the beginning and end of the string.

    Fix: Read the characters between the boundary marks as the string's content.

  • Failing to notice that a string can contain several words.

    A string is a sequence of characters and can be a bunch of words.

    Fix: Treat the complete sequence between the quotation marks as one string value.

Practice Check

EASY

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

  1. A string is a sequence of characters.
  2. Strings are commonly used to represent words and other text.
  3. Quotation marks specify the beginning and end of a string.
  4. The characters between the quotation marks are the string's content.
  5. Values such as 5 and 1.23 are numeric literals, while quoted text is a string literal.

Key Takeaways

  • 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.