Chapter 4 : Handling strings

Example 4.1, Page No 63

In [2]:
text = "9"
term = "9 "
info = "Toys"
hue = ['R','e','d','\0']
info = " Balloons"
color = "".join(hue)
text = text + term + color + info
print text
99 Red Balloons

Example 4.2, Page No 64

In [3]:
name = raw_input("Please enter your full name : ")
print "Welcome ",name
name = raw_input("Please re-enter your full name : ")
print "Thanks, ",name
Please enter your full name : Mike McGrath
Welcome  Mike McGrath
Please re-enter your full name : Mike McGrath
Thanks,  Mike McGrath

Example 4.3, Page No 66

In [6]:
term = "100"
number = 100
stream = term
num = stream
num = number / 4
print "Integer value: ",num
stream = ""
stream = number
text = stream
text = str(text) + " Per Cent"
print "String value: ",text
Integer value:  25
String value:  100 Per Cent

Example 4.4, Page No 68

In [1]:
#There is no such functions in python like  size, capacity and empty 

Example 4.5, Page No 70

In [5]:
lang = "C++"
term = "Programming"
text = "C++ Programming"
print "Concatinated: ",lang + term
print "Original: ",lang
#print "Appended: ",lang.append(term) there in no append method for string in python
print "Original: ",lang
print "Differ: ",(lang==term)
print "Match: ",(lang==text)
# print "Match: ",(lang.compare(text)) there in no compare method for string in python
# print "Differ: ",(lang.compare(term)) there in no compare method for string in python
# print "Lower ASCII: ",lang.compare("zzzzz") there in no compare method for string in python
 Concatinated:  C++Programming
Original:  C++
Original:  C++
Differ:  False
Match:  False

Example 4.6, Page No 72

In [7]:
text = "Always laugh when you can. It\’s cheap medicine."
front = text
print "Front: ",front
front = text
print "Front: ",front
back = text
print "Back: ",back
back = front
print "Front: ",front
print "Back: ",back
#There is no option to assing specific length in python also no swap function
Front:  Always laugh when you can. It\’s cheap medicine.
Front:  Always laugh when you can. It\’s cheap medicine.
Back:  Always laugh when you can. It\’s cheap medicine.
Front:  Always laugh when you can. It\’s cheap medicine.
Back:  Always laugh when you can. It\’s cheap medicine.

Example 4.7, Page No 74

In [23]:
text = "I can resist anything but temptation."
num = text.find("resist",0)
print "Position: ",num
num= text.find("nonsuch",0)
print "Result: ",num
num= text.find("I",0)
print "First I",num
num= text.find("i",text.find("I",0))
print "First not I",num
num= text.rfind("t",0)
print "Last t: ",num
num= text.rfind("t",text.rfind("t",0))
print "Last not t: ",num
Position:  6
Result:  -1
First I 0
First not I 9
Last t:  32
Last not t:  32

Example 4.8, Page No 76

In [46]:
text = "I do like the seaside"
print "Original: ",text
# text.insert(10,"to be aside") There is no insert method in python
print "Inserted: ",text
# text.erase(2,3) There is no erase method in python
print "Erased: ",text
text = text.replace("the seaside","strolling by the sea")
print "Replaced: ",text
print "Copied: ",text[10:19]
print "Last character: ",text[len(text)-1]
Original:  I do like the seaside
Inserted:  I do like the seaside
Erased:  I do like the seaside
Replaced:  I do like strolling by the sea
Copied:  strolling
Last character:  a