{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 2.5 Triple-Quoted Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Including Quotes in Strings" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Display \"hi\" in quotes')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Display 'hi' in quotes')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Display \\'hi\\' in quotes')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"Display the name O'Brien\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"Display \\\"hi\\\" in quotes\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"\"\"Display \"hi\" and 'bye' in quotes\"\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiline Strings" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "triple_quoted_string = \"\"\"This is a triple-quoted\n", "string that spans two lines\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(triple_quoted_string)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "triple_quoted_string" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A triple-quoted string allows you to avoid using escape sequences. They are \"as-is\" strings." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "another_triple_quoted_string = \"\"\"This triple-quoted string\n", "includes the line feeds.\n", "It’s even possible to include\n", "tabs and quotes, straight or\n", "“fancy”, all without using\n", "escape sequences.\"\"\"\n", "\n", "print(another_triple_quoted_string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **EXERCISE:** What displays when you execute the following statement?\n", "\n", "````\n", "print(\"\"\"This is a lengthy\n", " multiline string containing \n", "a few lines \\\n", "of text\"\"\")\n", "````" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) copyright 2023 by Ronald Mak" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 4 }