{ "metadata": { "name": "", "signature": "sha256:cc6cd32b3760ef9f8d727f2cbad62d4127abffa00d3c9ad693debfc7914ad190" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 6: Applications with Strings and Text" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6.1, page no. 221" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"The character \\\\0 is used to terminate a string.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The character \\0 is used to terminate a string.\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6.2, page no. 223" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "str1 = \"To be or not to be\"\n", "str2 = \",that is a question\"\n", "\n", "print \"Length of the string \\\"\", str1, \"\\\" is \", len(str1), \" characters\"\n", "print \"Length of the string \\\"\", str2, \"\\\" is \", len(str2), \" characters\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Length of the string \" To be or not to be \" is 18 characters\n", "Length of the string \" ,that is a question \" is 19 characters\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6.3, page no. 225" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "str1 = \"Computers do what you tell them to do, not what you want them to do.\"\n", "str2 = \"When you put something in memory, remember where you put it.\"\n", "str3 = \"Never test for a condition you don't know what to do with.\"\n", "\n", "print \"There are 3 strings.\"\n", "print \"The string: \"\n", "print str1\n", "print \"contains \", len(str1), \" characters\"\n", "print \"The string: \"\n", "print str2\n", "print \"contains \", len(str3), \" characters\"\n", "print \"The string: \"\n", "print str3\n", "print \"contains \", len(str3), \" characters\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "There are 3 strings.\n", "The string: \n", "Computers do what you tell them to do, not what you want them to do.\n", "contains 68 characters\n", "The string: \n", "When you put something in memory, remember where you put it.\n", "contains 58 characters\n", "The string: \n", "Never test for a condition you don't know what to do with.\n", "contains 58 characters\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6.4, page no. 230" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "preamble = \"The joke is: \"\n", "str1 = \"My dog hasn\\'t got any nose.\\n\"\n", "str2 = \"How does your dog smell then?\\n\"\n", "str3 = \"My dog smells horrible.\\n\"\n", "joke = str1 + str2 + str3\n", "\n", "print preamble\n", "print joke" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The joke is: \n", "My dog hasn't got any nose.\n", "How does your dog smell then?\n", "My dog smells horrible.\n", "\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6.5, page no. 234" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Type in the first word (maximum 20 characters): \",\n", "word1 = raw_input()\n", "print \"Type in the second word (maximum 20 characters): \",\n", "word2 = raw_input()\n", "\n", "if(word1 == word2):\n", " print \"You have entered identical words\"\n", "else:\n", " print \"%s precedes %s\\n\" %((word2 if(word1 > word2) else word1), (word1 if(word1 > word2) else word2)) " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6.6, page no. 240 " ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "str1 = \"This string contains the holy grail.\"\n", "str2 = \"the holy grail\"\n", "str3 = \"the holy grill\"\n", " \n", "if str2 in str1:\n", " print \"\\\"%s\\\" was found in \\\"%s\\\"\\n\" %(str2, str1)\n", "else:\n", " print \"\\n\\\"%s\\\" was not found.\" %str2\n", " \n", "if not(str3 in str1):\n", " print \"\\\"%s\\\" was not found.\\n\" % str3\n", "else:\n", " print \"\\nWe shouldn't get to here!\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\"the holy grail\" was found in \"This string contains the holy grail.\"\n", "\n", "\"the holy grill\" was not found.\n", "\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6.7, page no. 243" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter some prose that is less than 1000 characters (go on typing hit enter to terminate):\"\n", "str1 = raw_input()\n", "\n", "if len(str1) > 1000:\n", " print \"Maximum permitted input length exceeded.\"\n", "\n", "list_str1 = str1.split(\" \")\n", "\n", "print \"The words in the prose that you entered are: \"\n", "\n", "for i in range(0, len(list_str1)):\n", " print list_str1[i], \"\\t\",\n", " if i % 5 == 0:\n", " print \"\\n\"\n", "\n", "print \"Words found \", len(list_str1)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter some prose that is less than 1000 characters (go on typing hit enter to terminate):\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "y father's family name being Pirrip, and my Christian name Philip, my infant tongue could make of both names nothing longer or more explicit than Pip. So, I called myself Pip, and came to be called Pip.\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The words in the prose that you entered are: \n", "y \t\n", "\n", "father's \tfamily \tname \tbeing \tPirrip, \t\n", "\n", "and \tmy \tChristian \tname \tPhilip, \t\n", "\n", "my \tinfant \ttongue \tcould \tmake \t\n", "\n", "of \tboth \tnames \tnothing \tlonger \t\n", "\n", "or \tmore \texplicit \tthan \tPip. \t\n", "\n", "So, \tI \tcalled \tmyself \tPip, \t\n", "\n", "and \tcame \tto \tbe \tcalled \t\n", "\n", "Pip. \tWords found 37\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6.8, page no. 249" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "\n", "nLetters = 0\n", "nDigits = 0\n", "nPunct = 0\n", "print \"Enter an interesting string of less than 100 characters: \"\n", "str1 = raw_input()\n", "\n", "for char in str1:\n", " if char.isalpha():\n", " nLetters += 1\n", " elif char.isdigit():\n", " nDigits += 1\n", " elif char == \" \":\n", " pass\n", " else:\n", " nPunct += 1\n", "\n", "print \"Your string contained %d letters, %d digits and %d punctuation characters. \" %(nLetters, nDigits, nPunct)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter an interesting string of less than 100 characters: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "I was born on the 3rd of October 1895, which is long ago.\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Your string contained 38 letters, 5 digits and 2 punctuation characters. \n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6.9, page no. 251" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter the string to be searched (less than 100 characters): \",\n", "text = raw_input()\n", " \n", "print \"Enter the string sought (less than 40 characters):\",\n", "substring = raw_input()\n", " \n", "print \"First string entered: \", text\n", "print \"Second string entered: \", substring\n", " \n", "text = text.upper()\n", "substring = substring.upper() \n", "print \"The second string %s found in the first. \" %(\"was not\" if not substring in text else \"was\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the string to be searched (less than 100 characters): " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Cry havoc, and let slip the dogs of war.\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter the string sought (less than 40 characters):" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "The Dogs of War\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " First string entered: Cry havoc, and let slip the dogs of war.\n", "Second string entered: The Dogs of War\n", "The second string was found in the first. \n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 6.10, page no. 259" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "import numpy\n", "\n", "print \"Enter text (hit enter to exit): \",\n", "text = raw_input()\n", "\n", "text_split = text.split()\n", "distinct_words = []\n", "\n", "for i in text_split:\n", " if i not in distinct_words:\n", " distinct_words.append(i)\n", "\n", "word_count = numpy.zeros(len(distinct_words))\n", "\n", "for i in range(len(distinct_words)):\n", " for j in range(len(text_split)):\n", " if distinct_words[i] == text_split[j]:\n", " word_count[i] += 1\n", "\n", "for i in range(len(distinct_words)):\n", " print distinct_words[i], int(word_count[i])\n", " if i == 5:\n", " print \"\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter text (hit enter to exit): " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "When I makes tea I makes tea, as old mother Grogan said. And when I makes water I makes water. Begob, ma'am, says Mrs Cahill, God send you don't make them in the same pot.\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " When 1\n", "I 4\n", "makes 4\n", "tea 1\n", "tea, 1\n", "as 1\n", "\n", "old 1\n", "mother 1\n", "Grogan 1\n", "said. 1\n", "And 1\n", "when 1\n", "water 1\n", "water. 1\n", "Begob, 1\n", "ma'am, 1\n", "says 1\n", "Mrs 1\n", "Cahill, 1\n", "God 1\n", "send 1\n", "you 1\n", "don't 1\n", "make 1\n", "them 1\n", "in 1\n", "the 1\n", "same 1\n", "pot. 1\n" ] } ], "prompt_number": 21 } ], "metadata": {} } ] }