Overview
This lecture explains triple quoted strings in Python, their usage for multi-line strings, documentation (docstrings), and highlights issues with stylized quotes in code editors.
Creating Strings in Python
- Strings can be created with single (' ') or double (" ") quotes.
- Triple quoted strings start and end with three single (''') or double (""") quotes.
- Triple quotes allow for multi-line strings and embedding both single and double quotes without escape sequences.
Uses of Triple Quoted Strings
- Ideal for multi-line strings spanning several lines.
- Useful when a string contains both single and double quotes.
- Used for "docstrings"—documentation within Python functions.
Example Behaviors of Triple Quoted Strings
- Indentation within a triple quoted string is preserved in the output.
- You can include escape sequences (like \n for a new line) in triple quoted strings.
- No need to escape single or double quotes within a triple quoted string.
Docstrings in Python
- Docstrings are triple quoted strings placed as the first statement in a function to document its purpose.
- Python's help system returns docstrings for user-defined functions.
- Docstrings appear as tooltips or documentation in code editors like Spyder.
Stylized Quotes and Coding Errors
- Stylized quotes (curly quotes) differ from standard straight quotes used in code.
- Copying stylized quotes from slide decks or websites into code can cause errors (e.g., "invalid character in identifier").
- Python only recognizes specific straight quote characters from the ASCII table.
- Code editors may highlight stylized quotes as errors and they can disrupt string literals.
Key Terms & Definitions
- Triple Quoted String — A string in Python defined by three consecutive single or double quotes, supporting multi-line text and embedded quotes.
- Docstring — A triple quoted string at the start of a function, providing documentation accessible via help functions.
- Stylized Quotes — Curly quotation marks that differ from standard ASCII quotes and are not recognized by Python code.
Action Items / Next Steps
- Practice creating multi-line and docstrings using triple quotes in Python.
- Avoid copying stylized quotes from non-code sources into your Python code.
- Review the ASCII table for valid quote characters.