{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings\n", "\n", "## Conceptual Definition\n", "Though we usually turn to computers to work with numbers, we can also use them\n", "to work with alphabets, symbols, and/or numbers- in other words, a sequence of\n", "various characters that are are formally called _strings_.\n", "\n", "## Basics of strings\n", "Python, like most programming languages, can manipulate strings. Strings can\n", "be enclosed in single quotes (`'...'`) or double quotes (`\"...\"`) with the\n", "same result. For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'spam eggs' # Single quotes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you need to use a quote within the string itself, (e.g., to write words\n", "involving apostrophes), then do one of the following:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'doesn\\'t' # Use \\' to escape the single quote..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"doesn't\" # ...or use double quotes instead." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This use of backslash (`\\`) allows one to *escape quotes* for all special\n", "characters.\n", "This is sufficient for us to work with [the Vectors Tutorial](NB_Tutorial_Vectors_ReferenceFrames.ipynb).\n", "\n", "## Basics of strings (contd.)\n", "In the Jupyter notebooks, the output string is enclosed in quotes. The string is\n", "enclosed in double quotes\n", "if the string contains a single quote and no double quotes, otherwise it's\n", "enclosed in single quotes. The\n", "[`print()`](https://docs.python.org/3.6/library/functions.html#print) function\n", "produces a more readable output by omitting the enclosing quotes and by printing\n", "escaped and special characters:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'\"Isn\\'t,\" she said.'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('\"Isn\\'t,\" she said.')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = 'First line.\\nSecond line.' # \\n means newline.\n", "s # Without print(), \\n is included in the output." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(s) # With print(), \\n produces a new line." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you don't want escaped characters (prefaced by `\\`) to be interpreted as\n", "special characters, use *raw strings* by adding an `r` before the first quote:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('C:\\some\\name') # Here \\n means newline!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(r'C:\\some\\name') # Note the r before the quote." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.6" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }