{ "metadata": { "name": "", "signature": "sha256:cebcc94856d8ecdf1ae320585ba1942c4e6ccd608bed15be183214bd1392f3a2" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hour 13: Manipulating String" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13.1, Page No 210" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str1=['A',' ','s','t','r','i','n','g',' ','c','o','n','s','t','a','n','t']\n", "str2= \"Another string constant\"\n", "\n", "i=0\n", "for ch in str1:\n", " print ch,\n", "print \"\"\n", "\n", "for ch in str2:\n", " print ch,\n", "\n", "print \"\"\n", "\n", "#there is no concept of pointer in Python\n", "ptr_str=\"Assign a string to a pointer\"\n", "for ele in ptr_str:\n", " print ele," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " A s t r i n g c o n s t a n t \n", "A n o t h e r s t r i n g c o n s t a n t \n", "A s s i g n a s t r i n g t o a p o i n t e r\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13.2, Page No 212" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str1=['A',' ','s','t','r','i','n','g',' ','c','o','n','s','t','a','n','t']\n", "str2= \"Another string constant\"\n", "ptr_str=\"Assign a string to a pointer\"\n", "\n", "print \"The length of str1 is: %d bytes\\n\" % len(str1)\n", "print \"The length of str2 is: %d bytes\\n\" % len(str2)\n", "print \"The length of the string assigned to a pointer is: %d bytes\" % len(ptr_str)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The length of str1 is: 17 bytes\n", "\n", "The length of str2 is: 23 bytes\n", "\n", "The length of the string assigned to a pointer is: 28 bytes\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13.3, Page No 213" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str1=\"Copy a string\"\n", "str2=str1\n", "str3=range(len(str1))\n", "\n", "for i in range(len(str1)):\n", " str3[i]=str1[i]\n", " \n", "str3[i]='\\0'\n", "print \"The content of str2 using strcpy: %s\" % str2\n", "print \"The contect of str3 without using strcpy: %s\" % str3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The content of str2 using strcpy: Copy a string\n", "The contect of str3 without using strcpy: ['C', 'o', 'p', 'y', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', '\\x00']\n" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13.4, Page No 216" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str1 = range(80)\n", "delt=ord('a')-ord('A')\n", "\n", "str1 = raw_input(\"Enter a string less than 80 characters: \\n\")\n", "str1 = list(str1)\n", "i=0\n", "while(i!=len(str1)):\n", " if((str1[i]>='a') and (str1[i]<='z')):\n", " str1[i] = chr(ord(str1[i])-delt)\n", " i=i+1\n", "print \"The entered string is (in uppercase)\\n\"\n", "print \"\".join(str1)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a string less than 80 characters: \n", "This is a test.\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The entered string is (in uppercase)\n", "\n", "THIS IS A TEST.\n" ] } ], "prompt_number": 60 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13.5, Page No 218" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=int(raw_input(\"Enter integer:\"))\n", "y=int(raw_input(\"Enter integer:\"))\n", "z=float(raw_input(\"Enter a floating-point number:\"))\n", "str1=raw_input(\"Enter a string:\")\n", "print \"Here are what you've entered\"\n", "print \"%d \" % x,\n", "print \"%d \" % y\n", "print \"%f \" % z\n", "print \"%s \" % str1" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter integer:10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter integer:12345\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a floating-point number:1.234567\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a string:Test\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Here are what you've entered\n", "10 12345 \n", "1.234567 \n", "Test \n" ] } ], "prompt_number": 5 } ], "metadata": {} } ] }