Chapter 8 Strings

Example 8.1, Page No. 181

In [6]:
myString="MIKE"
print "The pointer variable's value is: ", id(myString)
print "The pointer variable points to: ",myString
print "The memory locations for each character are::"
for x in range(0,4):
    print id(myString[x]),
 The pointer variable's value is:  44562688
The pointer variable points to:  MIKE
The memory locations for each character are::
19548160 20083536 20086752 20285008

Example 8.2, Page No.183

In [9]:
color=raw_input("Enter your favorite color: ")
print "You entered: ",color
Enter your favorite color: Blue
You entered:  Blue

Example 8.3, Page No.183

In [10]:
color=raw_input("Enter your favorite color: ")
print "You entered: ",color
Enter your favorite color: Black
You entered:  Black

Example 8.4, Page No. 185

In [7]:
names = []
names.append("Michael")
names.append("Sheila")
names.append("Spencer")
names.append("Hunter")
names.append("Kenya")
print "\nNames in pointer array of type char:\n"
for i in range(5):
    print names[i]
Names in pointer array of type char:

Michael
Sheila
Spencer
Hunter
Kenya

Example 8.5, Page No.186

In [13]:
colors = []
print "Enter 3 colors: "
for i in range(3):
    colors.append(raw_input())
print "Your entered: "
for i in range(3):
    print colors[i]
Enter 3 colors: 
blue
black
white
Your entered: 
blue
black
white

Example 8.6, Page No.187

In [16]:
str1="123.79"
str2="55"

print "String1 is:\"",str1,"\""
print "String2 is:\"",str2,"\""

x=float(str1)
y=int(str2)

print "String 1 converted to a float is ",round(x,2)
print "String 2 converted to an integer is ",y
String1 is:" 123.79 "
String2 is:" 55 "
String 1 converted to a float is  123.79
String 2 converted to an integer is  55

Example 8.7, Page No.188

In [18]:
str1="37"
str2="20"

print "String1 + String2 is ", str1+str2
String1 + String2 is  3720

Example 8.8, Page No. 189

In [19]:
str1="37"
str2="20"
iResult=int(str1)+int(str2)
print "String1 + String2 is ", iResult
String1 + String2 is  57

Example 8.9, Page No. 190

In [20]:
str1="Michael"
str2="Vine"
print "The length of string1 is ",len(str1)
print "The length of string2 is ",len(str2)
The length of string1 is  7
The length of string2 is  4

Example 8.10, Page No.191

In [32]:
def convertL(char):
    char=char.lower()
    print "The first name converted to lower case is: ",char
def convertU(char):
    char=char.upper()
    print "The first name converted to Upper case is: ",char
name1="Michael"
name2="Vine"

convertL(name1)
convertU(name2)
The first name converted to lower case is:  michael
The first name converted to Upper case is:  VINE

Example 8.11, Page No.193

In [33]:
str2="C Language"
str1=str2
print "String 1 now contains ", str1
String 1 now contains  C Language

Example 8.12, Page No.194

In [34]:
str1="Computer Science "
str2="is applied mathematics"
print str1+str2
Computer Science is applied mathematics

Exmaple 8.13, Page No.195

In [39]:
str1="A"
str2="A"
str3="!"
print str1
print str2
print str3
if str1==str2:
    print "Letter A is equal to letter A"
if str1>str3:
    print "Letter A is greater than character !"
if str3<str1:
    print "Character ! is greater than letter A"
A
A
!
Letter A is equal to letter A
Letter A is greater than character !
Character ! is greater than letter A

Exmaple 8.14, Page No.197

In [44]:
str1="Analyzing strings with with the strstr() function"
str2="ing"
str3="xyz"

print str1
print str2
print str3

if((str1.find(str2))!=-1):
    print "Str2 was found in str1"
else: 
    print "Str2 was not found in str1"

if((str1.find(str3))!=-1):
     print "Str3 was found in str1"
else: 
    print "Str3 was not found in str1"
Analyzing strings with with the strstr() function
ing
xyz
Str2 was found in str1
Str3 was not found in str1

Exmaple 8.15, Page No.198

In [1]:
import time
import os
from array import array


def checkAnswer(string1,string2):
    for x in range(len(string2)):
        string2=string2.upper()
    if(set(string2).intersection(string1.split())):
        print "Great job"
    else :
        print "Sorry no word found..."
   
starGame=[]
starGame.append("ADELANGUAGEFERVZOPIBMOU")
starGame.append("ZBPOINTERSKLMLOOPMNOCOT")
starGame.append("PODSTRINGGDIWHIEEICERLS")
starGame.append("YVCPROGRAMMERWQKNULTHMD")
starGame.append("UKUNIXFIMWXIZEQZINPUTEX")

displayed=0
startTime=0
os.system('cls')
print "word find"
startTime=int(round(time.time()*1000))
for x in range(0,5):
    while ((startTime+3)>int(round(time.time()*1000))):
        if displayed==0:
            print "find a word in:"
            print "",starGame[x]
            displayed=1
    os.system('cls')
    answer=raw_input("Enter word found:")
    checkAnswer(starGame[x],answer)
    displayed=0
    startTime=int(round(time.time()*1000))
word find
find a word in:
 ADELANGUAGEFERVZOPIBMOU
Enter word found:U
Sorry no word found...
find a word in:
 ZBPOINTERSKLMLOOPMNOCOT
Enter word found:T
Sorry no word found...
find a word in:
 PODSTRINGGDIWHIEEICERLS
Enter word found:S
Sorry no word found...
find a word in:
 YVCPROGRAMMERWQKNULTHMD
Enter word found:YV
Sorry no word found...
find a word in:
 UKUNIXFIMWXIZEQZINPUTEX
Enter word found:UKU
Sorry no word found...