{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\def\\CC{\\bf C}\n", "\\def\\QQ{\\bf Q}\n", "\\def\\RR{\\bf R}\n", "\\def\\ZZ{\\bf Z}\n", "\\def\\NN{\\bf N}\n", "$$\n", "# Strings and the Burrows-Wheeler Transform\n", "\n", "Sage/Python includes a builtin datastructure from strings.\n", "\n", "There are several ways to input strings. You can input a string using single quotes (') or double quotes (\"):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This is a string!'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"This is a string!\"\n", "s" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "So is this!" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = 'So is this!'\n", "print t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also input a string using three quotes (\"\"\" or '''). This is useful if you want to use both \" and ' in your string, or you want your string to span multiple lines:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "This is a multi-line\n", " string\n", "that includes 'single quotes'\n", " and \"double quotes\"." ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"\"\"\n", "This is a multi-line\n", " string\n", "that includes 'single quotes'\n", " and \"double quotes\".\n", "\"\"\"\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercises**\n", "\n", "1. Create and print the following string" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ " \\ | ( | ) / /" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " > \\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_ | | | | | I <3 Coffee! /-- | | | /--/ \\_\\_\\_\\_\\_\\_\\_\\_\\_\\_\\_/\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Without using cut-and-paste(!) *replace* the substring `I <3 Coffee!` with the substring `I <3 Tea!`.\n", "3. Print a copy of your string with all the letters capitalized (upercase).\n", "\n", "## Operations on strings\n", "\n", "Strings behave very much like lists. The table below summarizes their common operations.\n", "\n", ">
Operation | \n", ">Syntax for lists | \n", ">Syntax for strings | \n", ">
---|---|---|
Accessing a letter | \n", ">list[3] | \n",
"> string[3] | \n",
">
Slicing | \n", ">list[3:17:2] | \n",
"> string[3:17:2] | \n",
">
Concatenation | \n", ">list1 + list2 | \n",
"> string1 + sting2 | \n",
">
A copy | \n", ">list[:] | \n",
"> string[:] | \n",
">
A reversed copy | \n", ">list[::-1] | \n",
"> string[::-1] | \n",
">
Length | \n", ">len(list) | \n",
"> len(string) | \n",
">