{ "metadata": { "name": "", "signature": "sha256:3323f7a9a077b84fffdd30478d4b0d0b7f4032b86670a58b5b333d42bee04f15" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 10: Strings

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.3, Page Number: 10.3

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "st='Programming'\n", "print 'The string is %s' %st" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The string is Programming\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.4, Page Number: 10.4

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "st='Hello World'\n", "print 'The line is :'\n", "print st" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The line is :\n", "Hello World\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.6, Page Number: 10.5

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "strin='Welcome to python'\n", "for i in strin:\n", " print i," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "W e l c o m e t o p y t h o n\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.7, Page Number: 10.6

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "str1='Programming'\n", "len1=len(str1)\n", "print 'The length of the string is ',len1\n", "\n", "str2=str1\n", "print 'First string is %s and copied string is %s' %(str1,str2)\n", "\n", "str3='Computer'\n", "\n", "if str1==str3:\n", " print 'Both strings are equal'\n", "elif str1Example 10.8,Page Number: 10.7

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def strlength(str1):\n", " count=0\n", " for i in str1:\n", " count+=1\n", " \n", " return count\n", "def strcopy(src):\n", " dst=[]\n", " for i in src:\n", " dst.append(i)\n", " \n", " dst=''.join(dst)\n", " \n", " return dst\n", "\n", "\n", "str1='New Delhi'\n", "len1=strlength(str1)\n", "print 'The length of the string is ',len1\n", "\n", "str2=strcopy(str1)\n", "print 'First string is %s and copied string is %s' %(str1,str2)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The length of the string is 9\n", "First string is New Delhi and copied string is New Delhi\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.9,Page Number: 10.9

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def strcompare(str1,str2):\n", " \n", " len1=len(str1)\n", " len2=len(str2)\n", " \n", " if len1str2[i]:\n", " return 1\n", " \n", " return 0\n", "\n", "str1='Programming'\n", "str2='Computer'\n", "status=strcompare(str1,str2)\n", "if status==-1:\n", " print 'First string is lesser than second string'\n", "elif status==1:\n", " print 'First string is greater than second string'\n", "else:\n", " print 'Both strings ae equal'\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "First string is greater than second string\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.10, Page Number: 10.10

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def leftconcat(dst,src):\n", " dst=src+dst\n", " return dst\n", "\n", "def rightconcat(dst,src):\n", " dst=dst+src\n", " return dst\n", "\n", "str1='Hello'\n", "str2='Friends'\n", "\n", "tempstr=leftconcat(str2,str1)\n", "print 'The first string after left concatenation becomes ', tempstr\n", "\n", "tempstr=rightconcat(str2,str1)\n", "print 'The first string after right concatenation becomes', tempstr\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The first string after left concatenation becomes HelloFriends\n", "The first string after right concatenation becomes FriendsHello\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.11,Page Numbr: 10.12

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "str1='All good boys have bread'\n", "count=0\n", "\n", "for i in str1:\n", " if i=='a' or i=='e'or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U' :\n", " count+=1\n", " \n", "print 'Total number of vowels in a given text are ',count" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Total number of vowels in a given text are 8\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.12, Page Number: 10.13

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "ch1='A'\n", "ch2=ord(ch1)+3\n", "print chr(ch2)\n", "\n", "ch1=chr(ord(ch1)+1)\n", "print ch1\n", "\n", "print ord('a')\n", "print ord('l')\n", "\n", "val=ord(ch1)*ch2\n", "print val\n", "\n", "print chr(100)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "D\n", "B\n", "97\n", "108\n", "4488\n", "d\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.13, Page Number: 10.13

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "text='I am studying 6 Theory Papers & 4 practicals'\n", "\n", "len1=len(text)\n", "text=list(text)\n", "for i in xrange(0,len1):\n", " if text[i]>='a' and text[i]<='z':\n", " text[i]=chr(ord(text[i])+ord('A')-ord('a'))\n", " \n", " \n", "text=''.join(text)\n", "\n", "print 'The text after converting lowercase alphabets to uppercase is '\n", "print text" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The text after converting lowercase alphabets to uppercase is \n", "I AM STUDYING 6 THEORY PAPERS & 4 PRACTICALS\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.14, Page Number: 10.14

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "text='The programming is a systematic process'\n", "substr='pro'\n", "text_len=len(text)\n", "sub_len=len(substr)\n", "text=list(text)\n", "substr=list(substr)\n", "\n", "for i in xrange(0,text_len-sub_len+1):\n", " \n", " for j in xrange(0,sub_len):\n", " \n", " if text[i+j]==substr[j]:\n", " continue\n", " else:\n", " break\n", " \n", " if j==sub_len-1:\n", " print 'The substring is present from subscript %d onwards' %i\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The substring is present from subscript 4 onwards\n", "The substring is present from subscript 32 onwards\n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.15,Page number: 10.15

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def reorder(x):\n", "\n", " n=len(x)\n", " for item in range(0,n-1):\n", " for i in range(item+1,n):\n", " if x[item]>x[i]:\n", " temp=x[item]\n", " x[item]=x[i]\n", " x[i]=temp\n", "\n", "\n", " return\n", "\n", "x=['PACIFIC','ATLANTIC','INDIAN','CARIBBEAN','BERING','BLACK','RED','NORTH','BALTIC','CASPIAN']\n", "print 'Original list of strings :\\n\\n'\n", "\n", "for i in x:\n", " print \"String : \",i\n", "\n", "reorder(x)\n", "\n", "print \"\\nReodered list of strings : \\n\\n\"\n", "\n", "for i in x:\n", " print \"String : \",i\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Original list of strings :\n", "\n", "\n", "String : PACIFIC\n", "String : ATLANTIC\n", "String : INDIAN\n", "String : CARIBBEAN\n", "String : BERING\n", "String : BLACK\n", "String : RED\n", "String : NORTH\n", "String : BALTIC\n", "String : CASPIAN\n", "\n", "Reodered list of strings : \n", "\n", "\n", "String : ATLANTIC\n", "String : BALTIC\n", "String : BERING\n", "String : BLACK\n", "String : CARIBBEAN\n", "String : CASPIAN\n", "String : INDIAN\n", "String : NORTH\n", "String : PACIFIC\n", "String : RED\n" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 15 } ], "metadata": {} } ] }