{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings\n", "\n", "`string` is a python datatype, just like `int`, `bool`, or `list` are datatypes. The values of type `string` are sequences of characters, and they are written in python enclosed in quotes. These can be double or single quotes:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s0 = \"This is a string.\"\n", "s1 = 'This is also a string.'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python keeps the string *exactly* as you wrote it. That includes spaces and capitalization." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tHiS is a WEIRD strINg... ... ..\n" ] } ], "source": [ "s = \"tHiS is a WEIRD strINg... ... ..\"\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to have newline characters (i.e. line breaks) in your string, you can write it using *triple* quotes:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s0 = \"\"\"This string has a\n", "line break\"\"\"\n", "\n", "s1 = '''Maybe you want to write something\n", "that uses multiple lines...'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you do not want to use triple quotes, you can write the newline character itself (in one line), and when the string is printed that will be transformed into a linea break. The newline character is `\\n`." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is the first line\n", "This is the second line\n" ] } ], "source": [ "s = \"This is the first line\\nThis is the second line\"\n", "print(s) # This character ^^ is the line break." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if we want to have quotation marks inside our string?\n", "\n", "We can *escape* them! *Escaping* means preceeding the character with a backslash (\\\\)." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "And then he asked: \"How can have have quotes in strings?\"\n" ] } ], "source": [ "s_with_quotes = \"And then he asked: \\\"How can have have quotes in strings?\\\"\"\n", "print(s_with_quotes) # Open quotes ^^ closing quotes ^^ ^ last one is the closing string" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings are like lists\n", "\n", "We can access characters by indexing:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First character: P\n", "Last character: g\n" ] } ], "source": [ "s = \"Principles of Computing\"\n", "print(\"First character:\", s[0])\n", "print(\"Last character:\", s[-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and substrings can be accessed by slicing:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Characters from indices 2 to 6: incip\n" ] } ], "source": [ "s = \"Principles of Computing\"\n", "print(\"Characters from indices 2 to 6:\", s[2:7])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The number of characters can be obtained using `len`:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "23" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Principles of Computing\"\n", "len(s)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "`in` can find substrings:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Principles of Computing\"\n", "\"in\" in s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`count` can count the number a substring occurs:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Principles of Computing\"\n", "s.count(\"in\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that **capitalization is always respected**." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Principles of Computing\"\n", "s.count(\"comp\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can concatenate strings using +. Note that no extra characters (like spaces) are added." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PrinciplesofComputing\n" ] } ], "source": [ "s1 = \"Principles\"\n", "s2 = \"Computing\"\n", "print(s1 + \"of\" + s2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**BUT THEY CANNOT BE MODIFIED LIKE LISTS** (strings are immutable)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Principles of Computing\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"C\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "source": [ "s = \"Principles of Computing\"\n", "s[0] = \"C\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Looping through strings\n", "\n", "Like we do with lists on the range of the length:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Principles of Computing\"\n", "num_is = 0\n", "for i in range(len(s)):\n", " if s[i] == \"i\":\n", " num_is += 1\n", " \n", "num_is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or on each character:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Principles of Computing\"\n", "num_is = 0\n", "for c in s:\n", " if c == \"i\":\n", " num_is += 1\n", " \n", "num_is" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1\n", "\n", "Given a string `s` representing a piece of text, implement the function `words(s)` that returns a list containing all of the words in this text. You should also get rid of the punctuation marks: . , ! ?.\n", "\n", "For example, `words(\"Once upon a time in a land far far away...\")` should return:\n", "\n", "`[\"Once\", \"upon\", \"a\", \"time\", \"in\", \"a\", \"land\", \"far\", \"far\", \"away\"]`." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def words(s):\n", " return []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2\n", "\n", "Given a string `s`, implement the function `mostFrequent(s)` that returns the most frequent character.\n", "\n", "For example, `mostFrequent(\"exercise 2\")` should return `\"e\"`." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def mostFrequent(s):\n", " return \"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3\n", "\n", "Implement the function `combiner(s1, s2)` that takes two strings as parameters and combines them, alternating letters, starting with the first letter of the first String, followed by the first letter of the second String, then second letter of first String, etc. The remaining letters of the longer string are then appended to the end of the combination string and this combination string is returned.\n", "\n", "For example, `combiner(\"SaWr\", \"tras\")` should return `\"StarWars\"`." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def combiner(s1, s2):\n", " return \"\"" ] } ], "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.8.3" } }, "nbformat": 4, "nbformat_minor": 2 }