Strings & Text · Lesson 21

Create Strings with Quotes

A Python string literal creates a str object from text written between matching quotes.

ConceptWorked examplePracticeKnowledge check
Textbook walkthrough

Create Strings with Quotes

A Python string literal creates a str object from text written between matching quotes. Single and double quotes are equivalent delimiters; triple-quoted strings can span multiple lines. The delimiter is syntax and is not part of the resulting string value.

Learning goal: explain why Create Strings with Quotes behaves this way, apply it to a small example, and verify the result independently. Begin by being able to justify this first step: Choose single, double or triple quotes to delimit text without accidentally terminating it early.

Deeper walkthrough

Read Create Strings with Quotes as a mechanism, not a recipe

Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Choose single, double or triple quotes to delimit text without accidentally terminating it early. Stage 2: Use escaping only when a quote/backslash/newline must be represented inside the literal. Stage 3: Remember that the quote characters delimit the source literal; they are not normally part of the resulting string. Final checkpoint: Use raw strings selectively for backslash-heavy text, while remembering their own quoting rules.

Mechanism

Follow the transformation

Choose single, double or triple quotes to delimit text without accidentally terminating it early.

Use escaping only when a quote/backslash/newline must be represented inside the literal.

Remember that the quote characters delimit the source literal; they are not normally part of the resulting string.

Evidence

Know what would convince you

  • Trace a tiny input by hand and compare the runtime result.
  • Inspect type, value/shape and any mutation/side effect explicitly.
Useful distinctionInput: Objects/values supplied to the operation.
Click a stage to inspect what happens, what changes, and what should be checked before moving on.
Stage 1

Choose single

Choose single, double or triple quotes to delimit text without accidentally terminating it early. For Create Strings with Quotes, identify the exact state before this stage, the operation or rule applied here, and the observable state afterwards so the mechanism remains inspectable.

State focus: identify exactly what changed at this stage and what observable evidence confirms that change.
How it works

Trace the mechanism step by step

  1. Choose single, double or triple quotes to delimit text without accidentally terminating it early.
  2. Use escaping only when a quote/backslash/newline must be represented inside the literal.
  3. Remember that the quote characters delimit the source literal; they are not normally part of the resulting string.
  4. Inspect repr(text) when whitespace or escape characters are difficult to see.
  5. Use raw strings selectively for backslash-heavy text, while remembering their own quoting rules.
Worked demonstration

Create Strings with Quotes

# Step 1 — Compute the right-hand expression and store its result in `text` for the next step.
text = "data science"
# Step 2 — Compute the right-hand expression and store its result in `message` for the next step.
message = 'She said "hello"'
# Step 3 — Display the current value explicitly so the result/state can be inspected during execution.
print(text)
# Step 4 — Display the current value explicitly so the result/state can be inspected during execution.
print(message)
Expected / illustrative result
The quote characters delimit source literals; the printed strings contain only their intended text.
Interpret the result.

For Create Strings with Quotes, trace the specific input through the mechanism above and independently verify one returned value, state change or side effect.

Distinctions & related ideas

Place the concept correctly

InputObjects/values supplied to the operation.
StateNames or mutable objects that may change during execution.
OutputReturned value, side effect, file, plot or exception to inspect.
Use deliberately

When it is appropriate

Use Create Strings with Quotes when it answers a defined question in Strings & Text and its inputs/assumptions match the current data or program state.

Boundary conditions

When to stop or reconsider

Reconsider Create Strings with Quotes when the required information is unavailable, the operation would violate a validation/data boundary, or a simpler operation answers the question more transparently.

Common mistakes

Failure modes to recognise

  • Running the operation on the wrong object/type or in the wrong environment.
  • Inferring correctness from “no exception” without checking the produced value/state.
  • Hiding a boundary case instead of making its behaviour explicit.
Verification

How to check the result

  • Trace a tiny input by hand and compare the runtime result.
  • Inspect type, value/shape and any mutation/side effect explicitly.
  • Run an edge or invalid case and confirm the exception/behaviour is deliberate.
Hands-on practice

Demonstrate understanding

Try this:

Construct a tiny example of Create Strings with Quotes. First choose single, double or triple quotes to delimit text without accidentally terminating it early. Then use escaping only when a quote/backslash/newline must be represented inside the literal. Predict the result before execution and explain one boundary or failure case.

Use the smallest values that expose the language rule. Write the expected value and type first, then compare the actual state/output with that prediction.
Knowledge check

Check reasoning, not memorisation

Which approach best demonstrates understanding of Create Strings with Quotes?

Quick reference

Remember the logic

Step 1Choose single, double or triple quotes to delimit text without accidentally terminating it early.
Step 2Use escaping only when a quote/backslash/newline must be represented inside the literal.
Step 3Remember that the quote characters delimit the source literal; they are not normally part of the resulting string.
Step 4Inspect repr(text) when whitespace or escape characters are difficult to see.
Lesson summary

What to remember

  • A Python string literal creates a str object from text written between matching quotes. Single and double quotes are equivalent delimiters; triple-quoted strings can span multiple lines. The delimiter is syntax and is not part of the resulting string value.
  • Identify the Python objects and types involved.
  • Running the operation on the wrong object/type or in the wrong environment.
  • Trace a tiny input by hand and compare the runtime result.