From 9de92ea09272ebcf54c47bc990b993ae98764a6c Mon Sep 17 00:00:00 2001 From: Ashwin Maran <amaran@wisc.edu> Date: Sun, 18 Feb 2024 16:51:18 -0600 Subject: [PATCH] Upload New File --- .../13_Strings/Lec_13_Strings_Worksheet.ipynb | 322 ++++++++++++++++++ 1 file changed, 322 insertions(+) create mode 100644 s24/AmFam_Ashwin/13_Strings/Lec_13_Strings_Worksheet.ipynb diff --git a/s24/AmFam_Ashwin/13_Strings/Lec_13_Strings_Worksheet.ipynb b/s24/AmFam_Ashwin/13_Strings/Lec_13_Strings_Worksheet.ipynb new file mode 100644 index 0000000..068051e --- /dev/null +++ b/s24/AmFam_Ashwin/13_Strings/Lec_13_Strings_Worksheet.ipynb @@ -0,0 +1,322 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem 1" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True True False\n", + "False False False\n", + "False True True\n", + "True False True\n", + "False True True\n" + ] + } + ], + "source": [ + "print(\"a\" < \"z\", \"ax\" < \"ay\", \"abc\" < \"abCd\")\n", + "print(\"a\" < \"Z\", \"x2\" < \"x1\", \"zero\" < \"999\")\n", + "print(\"x\" < \"x\", \"abcX\" < \"abcY\", \"10\" < \"999\")\n", + "print(\"0\" < \"x\", \"abcX\" < \"aBcY\", \"1000\" < \"999\")\n", + "print(\"1\" < \"0\", \"abc\" < \"abcd\", \"88888888888\" < \"9\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem 2 left column" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "DOG\n", + "dog\n", + "paint\n", + " paint\n", + "True\n" + ] + } + ], + "source": [ + "print(\"dog\".upper())\n", + "print(\"Dog\".lower())\n", + "print(\" paint \".strip())\n", + "print(\" paint \".rstrip())\n", + "print(\"abcd\".startswith(\"ab\"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem 2 right column" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "0\n", + "2\n", + "-1\n", + "2\n" + ] + } + ], + "source": [ + "print(\"abcd\".endswith(\"bc\"))\n", + "print(\"abcd\".find(\"a\"))\n", + "print(\"abcd\".find(\"c\"))\n", + "print(\"abcd\".find(\"B\"))\n", + "print(\"Python\".find(\"th\"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem 3" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a\n", + "c\n", + "c\n", + "o\n", + "3\n", + "=\n", + "um\n" + ] + } + ], + "source": [ + "msg = \"Hello\"\n", + "x = \"num= 13\" # let's assume there is a space after the =\n", + "\n", + "print(\"abc\"[0])\n", + "print(\"abc\"[2])\n", + "print(\"abc\"[-1])\n", + "\n", + "print(msg[4])\n", + "# print(msg[5]) # will cause error - see following cells\n", + "# print(msg[len(msg)]) # will cause error - see following cells\n", + "\n", + "print(x[len(x) - 1])\n", + "print(x[3])\n", + "print(x[1] + x[2])" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "string index out of range", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[5], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(msg[\u001b[38;5;241m5\u001b[39m])\n", + "\u001b[1;31mIndexError\u001b[0m: string index out of range" + ] + } + ], + "source": [ + "print(msg[5])" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "string index out of range", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[6], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(msg[\u001b[38;5;28mlen\u001b[39m(msg)])\n", + "\u001b[1;31mIndexError\u001b[0m: string index out of range" + ] + } + ], + "source": [ + "print(msg[len(msg)])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem 4" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ab\n", + "cde\n", + "cde\n", + "He\n", + "llo\n", + "lo\n", + "Hell\n", + "Hello\n", + "ello\n" + ] + } + ], + "source": [ + "p = \"= \" \n", + "\n", + "print(\"abcde\"[0:2])\n", + "print(\"abcde\"[2:6])\n", + "print(\"abcde\"[2:9])\n", + "\n", + "print(msg[:2])\n", + "print(msg[2:])\n", + "print(msg[-2:])\n", + "\n", + "print(msg[:msg.find('=')])\n", + "print(msg[msg.find(' ')+1:])\n", + "print(msg[msg.find(p)+len(p):])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem 5" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "301\n", + "301\n", + "301\n", + "3.0.1.\n", + "103\n" + ] + } + ], + "source": [ + "msg = \"301\"\n", + "A = \"\"\n", + "B = \"\"\n", + "\n", + "for character in msg:\n", + " print(msg)\n", + " A = A + character + \".\"\n", + " B = character + B\n", + "\n", + "# What is in A afterwards?\n", + "print(A)\n", + "\n", + "# What is in B afterwards?\n", + "print(B)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem 6" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "P\n", + "PY\n", + "PYT\n", + "PYTH\n", + "PYTHO\n", + "PYTHON\n" + ] + } + ], + "source": [ + "s = \"PYTHON\"\n", + "for i in range(len(s)):\n", + " print(s[:i+1])" + ] + } + ], + "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.11.7" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} -- GitLab