๐Ÿ“

Python Triple-Quoted Strings

Oct 20, 2025

Overview

This lecture explains the use of triple-quoted strings in Python, their applications (multi-line strings and docstrings), and highlights problems caused by stylized (curly) quotes in code.

Triple-Quoted Strings in Python

  • Strings can be created using three single quotes (''') or three double quotes (""") at both start and end.
  • Triple-quoted strings are ideal for multi-line strings and strings containing both single and double quotes.
  • Triple-quoted strings eliminate the need for escape characters for single or double quotes.
  • You can still use escape sequences (e.g., \n for a new line) within triple-quoted strings.

Uses of Triple-Quoted Strings

  • Best used for multi-line text blocks in Python code.
  • Allow inclusion of both single (') and double (") quotes without escaping.
  • Used for writing docstrings, which are documentation strings for functions, classes, or modules.

Docstrings and Help Functionality

  • Placing a triple-quoted string just after a function, class, or module definition creates a docstring.
  • Pythonโ€™s help() function displays the docstring as documentation for users.
  • IDEs like Spyder show docstrings as tooltips for built-in and user-defined functions.

Stylized Quotes and Code Errors

  • Stylized (curly) quotes (โ€˜ โ€™ or โ€œ โ€) from word processors or slide decks are different from standard code quotes.
  • Python does not recognize stylized quotes as string delimiters, causing syntax errors like โ€œinvalid character in identifier.โ€
  • Copying code from materials with stylized quotes can lead to bugs or errors; always use standard ASCII single or double quotes in code.

Key Terms & Definitions

  • Triple-quoted string โ€” A string enclosed with three consecutive quotes, used for multiline text or docstrings.
  • Docstring โ€” A special triple-quoted string that documents a function, class, or module in Python.
  • Stylized quotes โ€” Non-standard quote characters (curly or slanted) that are not recognized as string delimiters in Python.

Action Items / Next Steps

  • Practice writing multi-line strings and docstrings using triple quotes in Python.
  • Always use standard ASCII quotes for strings in code editors.
  • Be cautious when copying code from formatted documents or websites to avoid stylized quote errors.

Absolutely! Letโ€™s make triple-quoted strings in Python quick and fun! ๐Ÿฅณ


๐ŸŽ‰ Triple-Quoted Strings: The Super String Wrappers! ๐ŸŽ‰

What are they?
Strings wrapped in three quotes โ€” either ''' or """ โ€” at the start and end.


Why use them? ๐Ÿค”

  • Multi-line magic!
    Write strings that span multiple lines without \n everywhere.

  • Quote party!
    Your string can have both 'single' and "double" quotes inside without freaking out or needing backslashes.

  • Docstring power!
    Use triple quotes right after a function to explain what it does โ€” Pythonโ€™s way of adding helpful notes!


Quick Example ๐Ÿš€

# Multi-line string with triple quotes
poem = """Roses are red,
Violets are blue,
I love Python,
And so do you!"""

print(poem)

Output:

Roses are red,
Violets are blue,
I love Python,
And so do you!

Docstring Example ๐Ÿ“

def greet(name):
    """Say hello to someone by name."""
    print(f"Hello, {name}!")

help(greet)  # Shows the docstring!

Watch Out! โš ๏ธ

  • No curly quotes!
    Donโ€™t copy-paste quotes like โ€œ or โ€˜ from fancy docs โ€” Python hates those! Use straight quotes " or '.

Quick Recap ๐ŸŽฏ

  • Triple quotes = multi-line + mixed quotes, no fuss!
  • Great for docstrings = your codeโ€™s personal assistant.
  • Avoid curly quotes = keep Python happy!

Ready to try? Just wrap your text in """ or ''' and watch the magic happen! โœจ

Want me to help you write some triple-quoted strings?