{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e1a268a7",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# String Processing and Formatting\n",
    "#### CS 65: Introduction to Computer Science I"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0696b12b",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Review: Useful string methods we've used\n",
    "\n",
    "* `rstrip()` - strip out newlines/whitespace at the end of a string\n",
    "* `split()` - split a string into a list of strings based on a delimeter\n",
    "* `index()` - find a substring inside a string\n",
    "* `count()` - count the number of times a substring appears in a string"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf4d0ba5",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Other string methods to know about\n",
    "It's always good to be aware of a whole bunch of string methods - they can come in very handy.\n",
    "\n",
    "See https://www.w3schools.com/python/python_ref_string.asp\n",
    "\n",
    "Here's a sample:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c0845047",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name = \"Eric\"\n",
    "#check if all the characters are lowercase\n",
    "name.islower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7c121bab",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'eric'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lowercase_name = name.lower()\n",
    "lowercase_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0007d7f2",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name1 = \"eric\"\n",
    "name2 = \"Titus\"\n",
    "name1.lower() < name2.lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4b1a7809",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Eric'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name1 = \"eric\"\n",
    "name1.capitalize()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcdea34d",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Here's a potential problem with user input:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f90bfacb",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a number: ten\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "could not convert string to float: 'ten'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-5-63a2d6e7a2b7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0muser_input\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter a number: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Your number divided by 2 is\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0muser_input\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: could not convert string to float: 'ten'"
     ]
    }
   ],
   "source": [
    "user_input = float(input(\"Enter a number: \"))\n",
    "print(\"Your number divided by 2 is\",user_input/2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f57ddada",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Here's a fix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1774a993",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a number: ten\n",
      "That's not a number!\n"
     ]
    }
   ],
   "source": [
    "user_input = input(\"Enter a number: \")\n",
    "if user_input.isnumeric():\n",
    "    user_input = float(user_input)\n",
    "    print(\"Your number divided by 2 is\",user_input/2)\n",
    "else:\n",
    "    print(\"That's not a number!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29e5a4a4",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Some formatting annoyances\n",
    "\n",
    "Perhaps you have tried to do something like this, and you end up with a space between the $ and the number that you don't want."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d8903ab6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Employee 123 made $ 980.543 this period.\n"
     ]
    }
   ],
   "source": [
    "employee_num = 123\n",
    "pay = 980.543\n",
    "print(\"Employee\",employee_num,\"made $\",pay,\"this period.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ede8d0ef",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## The `format()` method\n",
    "\n",
    "The `format()` method allows you to substitute values into placeholders within a string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "216db29a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Employee 123 made $980.543 this pay period.\n"
     ]
    }
   ],
   "source": [
    "employee_num = 123\n",
    "pay = 980.543\n",
    "pay_message = \"Employee {n} made ${p} this pay period.\"\n",
    "print( pay_message.format(n=employee_num, p=pay) )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb89d3bd",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "You can even do it in one step:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d4ad89c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Employee 123 made $980.543 this pay period.\n"
     ]
    }
   ],
   "source": [
    "employee_num = 123\n",
    "pay = 980.543\n",
    "print( \"Employee {n} made ${p} this pay period.\".format(n=employee_num, p=pay) )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "140783e6",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "You can even add formatting information to the placeholders for things like how to format numbers.\n",
    "\n",
    "In this example `\":.2f\"` means format as a floating-point number with 2 places to the right of the decimal point."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1bb729e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Employee 123 made $980.54 this pay period.\n"
     ]
    }
   ],
   "source": [
    "employee_num = 123\n",
    "pay = 980.543\n",
    "print( \"Employee {n} made ${p:.2f} this pay period.\".format(n=employee_num, p=pay) )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "339115bc",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "`\":.0%\"` means format as a percentage with 0 decimal places"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "12261984",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eric earned a 90% on the test\n"
     ]
    }
   ],
   "source": [
    "\n",
    "print(\"{name} earned a {perc_grade:.0%} on the test\".format(name=\"Eric\",perc_grade=.897))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e92d2138",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### How can you know how to use all of these things?\n",
    "\n",
    "You just have to know it's a thing, and look it up when you need to use it.\n",
    "\n",
    "https://docs.python.org/3/library/string.html#formatstrings\n",
    "\n",
    "https://www.w3schools.com/python/ref_string_format.asp"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1069ee6",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Review: Character representation\n",
    "\n",
    "Every text character is represented by a numerical code in your computer's memory. \n",
    "\n",
    "They came up with in the 1960's as part of the ASCII standard (https://en.wikipedia.org/wiki/ASCII)\n",
    "\n",
    "Here's some examples\n",
    "\n",
    "<table>\n",
    "<tr>\n",
    "    <td style=\"padding:30px\">\n",
    "        <table>\n",
    "            <tr><th> code </th> <th>character </tr>\n",
    "            <tr><td>33 </td><td> !</td></tr>\n",
    "            <tr><td>34 </td><td> \"</td></tr>\n",
    "            <tr><td>35 </td><td> #</td></tr>\n",
    "            <tr><td>36 </td><td> $</td></tr>\n",
    "            <tr><td>37 </td><td> % </td></tr>      \n",
    "        </table>      \n",
    "    </td>\n",
    "    <td style=\"padding:30px\">\n",
    "        <table>\n",
    "            <tr><th> code </th> <th>character </tr>\n",
    "            <tr><td>65 </td><td> A</td></tr>\n",
    "            <tr><td>66 </td><td> B</td></tr>\n",
    "            <tr><td>67 </td><td> C</td></tr>\n",
    "            <tr><td>68 </td><td> D</td></tr>\n",
    "            <tr><td>69 </td><td> E </td></tr>      \n",
    "        </table>\n",
    "    </td>\n",
    "    <td style=\"padding:30px\">\n",
    "        <table>\n",
    "            <tr><th> code </th> <th>character </tr>\n",
    "            <tr><td>97 </td><td> a</td></tr>\n",
    "            <tr><td>98 </td><td> b</td></tr>\n",
    "            <tr><td>99 </td><td> c</td></tr>\n",
    "            <tr><td>100 </td><td> d</td></tr>\n",
    "            <tr><td>101 </td><td> e </td></tr>      \n",
    "        </table>     \n",
    "    </td\n",
    "</tr>\n",
    "</table>\n",
    "    \n",
    "You can use the built-in `ord()` function to look up the code for any given character. Use `chr()` to do the opposite."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a28db44b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "65"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ord(\"A\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f6a45e70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'A'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chr(65)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b923a5e",
   "metadata": {},
   "source": [
    "In the lab, we are going to use the functions to do some encryption on strings."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
