{ "metadata": { "name": "", "signature": "sha256:b79ee2681573b5fe1113810718887e0801a441cdcbacd23dfe8fbd78765cba80" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 4 : Handling strings" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.1, Page No 63" ] }, { "cell_type": "code", "collapsed": false, "input": [ "text = \"9\"\n", "term = \"9 \"\n", "info = \"Toys\"\n", "hue = ['R','e','d','\\0']\n", "info = \" Balloons\"\n", "color = \"\".join(hue)\n", "text = text + term + color + info\n", "print text" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "99 Red\u0000 Balloons\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.2, Page No 64" ] }, { "cell_type": "code", "collapsed": false, "input": [ "name = raw_input(\"Please enter your full name : \")\n", "print \"Welcome \",name\n", "name = raw_input(\"Please re-enter your full name : \")\n", "print \"Thanks, \",name" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Please enter your full name : Mike McGrath\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Welcome Mike McGrath\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Please re-enter your full name : Mike McGrath\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Thanks, Mike McGrath\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.3, Page No 66" ] }, { "cell_type": "code", "collapsed": false, "input": [ "term = \"100\"\n", "number = 100\n", "stream = term\n", "num = stream\n", "num = number / 4\n", "print \"Integer value: \",num\n", "stream = \"\"\n", "stream = number\n", "text = stream\n", "text = str(text) + \" Per Cent\"\n", "print \"String value: \",text" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Integer value: 25\n", "String value: 100 Per Cent\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.4, Page No 68" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#There is no such functions in python like size, capacity and empty " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.5, Page No 70" ] }, { "cell_type": "code", "collapsed": false, "input": [ "lang = \"C++\"\n", "term = \"Programming\"\n", "text = \"C++ Programming\"\n", "print \"Concatinated: \",lang + term\n", "print \"Original: \",lang\n", "#print \"Appended: \",lang.append(term) there in no append method for string in python\n", "print \"Original: \",lang\n", "print \"Differ: \",(lang==term)\n", "print \"Match: \",(lang==text)\n", "# print \"Match: \",(lang.compare(text)) there in no compare method for string in python\n", "# print \"Differ: \",(lang.compare(term)) there in no compare method for string in python\n", "# print \"Lower ASCII: \",lang.compare(\"zzzzz\") there in no compare method for string in python" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " Concatinated: C++Programming\n", "Original: C++\n", "Original: C++\n", "Differ: False\n", "Match: False\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.6, Page No 72" ] }, { "cell_type": "code", "collapsed": false, "input": [ "text = \"Always laugh when you can. It\\\u2019s cheap medicine.\"\n", "front = text\n", "print \"Front: \",front\n", "front = text\n", "print \"Front: \",front\n", "back = text\n", "print \"Back: \",back\n", "back = front\n", "print \"Front: \",front\n", "print \"Back: \",back\n", "#There is no option to assing specific length in python also no swap function" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Front: Always laugh when you can. It\\\u2019s cheap medicine.\n", "Front: Always laugh when you can. It\\\u2019s cheap medicine.\n", "Back: Always laugh when you can. It\\\u2019s cheap medicine.\n", "Front: Always laugh when you can. It\\\u2019s cheap medicine.\n", "Back: Always laugh when you can. It\\\u2019s cheap medicine.\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.7, Page No 74" ] }, { "cell_type": "code", "collapsed": false, "input": [ "text = \"I can resist anything but temptation.\"\n", "num = text.find(\"resist\",0)\n", "print \"Position: \",num\n", "num= text.find(\"nonsuch\",0)\n", "print \"Result: \",num\n", "num= text.find(\"I\",0)\n", "print \"First I\",num\n", "num= text.find(\"i\",text.find(\"I\",0))\n", "print \"First not I\",num\n", "num= text.rfind(\"t\",0)\n", "print \"Last t: \",num\n", "num= text.rfind(\"t\",text.rfind(\"t\",0))\n", "print \"Last not t: \",num" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Position: 6\n", "Result: -1\n", "First I 0\n", "First not I 9\n", "Last t: 32\n", "Last not t: 32\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.8, Page No 76" ] }, { "cell_type": "code", "collapsed": false, "input": [ "text = \"I do like the seaside\"\n", "print \"Original: \",text\n", "# text.insert(10,\"to be aside\") There is no insert method in python\n", "print \"Inserted: \",text\n", "# text.erase(2,3) There is no erase method in python\n", "print \"Erased: \",text\n", "text = text.replace(\"the seaside\",\"strolling by the sea\")\n", "print \"Replaced: \",text\n", "print \"Copied: \",text[10:19]\n", "print \"Last character: \",text[len(text)-1]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Original: I do like the seaside\n", "Inserted: I do like the seaside\n", "Erased: I do like the seaside\n", "Replaced: I do like strolling by the sea\n", "Copied: strolling\n", "Last character: a\n" ] } ], "prompt_number": 46 } ], "metadata": {} } ] }