# CHAPTER 9: PUPPETTING ON STRINGS¶

## EXAMPLE ON PAGE:311¶

In [4]:

name="Klinsman"
i=0
while i<=7:
print "%c" % (name[i]),                                  #method of printing without line feed
i+=1
print "\n"

K l i n s m a n



## EXAMPLE ON PAGE:311-312¶

In [14]:

name="Klinsman "
i=0
while name[i]!=' ':
print "%c" % (name[i]),
i+=1
print "\n"

 K l i n s m a n



## EXAMPLE ON PAGE:312¶

In [18]:

name="Klinsman "
ptr=[]
ptr=name                                        #store base address of string
i=0
while name[i]!=' ':
print "%c" % (ptr[i]),
i+=1
print "\n"

K l i n s m a n



## EXAMPLE ON PAGE:313¶

In [19]:

name="Klinsman"
print "%s" % (name)

Klinsman


## EXAMPLE ON PAGE:313¶

In [20]:

name=raw_input()
print "Hello %s!\n" % (name)

Enter your name
Debashish
Hello Debashish!



## EXAMPLE ON PAGE:314¶

In [21]:

name=raw_input()
print "Hello!"
print name

Enter your full name:
Debashish Roy
Hello!
Debashish Roy


## EXAMPLE ON PAGE:315¶

In [6]:

str1="Hello"
s="Good Morning"
str2=str1                         #No error in Python since all variables are by default pointers
q=s


## EXAMPLE ON PAGE:316¶

In [5]:

str1="Hello"
p="Hello"
str1="Bye"                                            #No error in python
p="Bye"


## EXAMPLE ON PAGE:317¶

In [22]:

arr="Bamboozled"
len1=len(arr)                                       #return length of string
len2=len("Humpty Dumpty")
print "string=%s length=%d\n" % (arr,len1)
print "string=%s length=%d\n" % ("Humpty Dumpty",len2)

string=Bamboozled length=10

string=Humpty Dumpty length=13



## EXAMPLE ON PAGE:317-318¶

In [23]:

def xstrlen(s):
length=0
for item in s:
if s!="\0":
length+=1
return length
arr="Bamboozled"
len1=xstrlen(arr)
len2=xstrlen("Humpty Dumpty")
print "string=%s length=%d\n" % (arr,len1)
print "string=%s length=%d\n" % ("Humpty dumpty",len2)

string=Bamboozled length=10

string=Humpty dumpty length=13



## EXAMPLE ON PAGE:319¶

In [24]:

import copy
source="Sayonara"
target=copy.copy(source)                              #copy string
print "source string=%s\n" % (source)
print "target string=%s\n" % (target)

source string=Sayonara

target string=Sayonara



## EXAMPLE ON PAGE:319-320¶

In [25]:

def xstrcpy(t,s):
t="\0"
for item in s:
t=t+str(item)
return t
source="Sayonara"
target=[]
target=xstrcpy(target,source)
print "source string=%s\n" % (source)
print "target string=%s\n" % (target)

source string=Sayonara

target string=Sayonara



## EXAMPLE ON PAGE:321¶

In [26]:

pi=3.14
r=eval(raw_input())
a=pi*r*r
print "Area of circle=%f\n" % (a)

Enter radius of circle
3
Area of circle=28.260000



## EXAMPLE ON PAGE:322¶

In [27]:

source="Folks!"
target="Hello"
target=target+source
print "source string=%s\n" % (source)
print "target string=%s\n" % (target)

source string=Folks!

target string=HelloFolks!



## EXAMPLE ON PAGE:322-323¶

In [29]:

string1="Jerry"
string2="Ferry"
i=cmp(string1,"Jerry")
j=cmp(string1,string2)
k=cmp(string1,"Jerry boy")
print "%d %d %d\n" % (i,j,k)

0 1 -1



## EXAMPLE ON PAGE:323-324¶

In [51]:

def FOUND():
return 1
def NOTFOUND():
return 0
masterlist=["akshay","parag","raman","srinivas","gopal","rajesh"]
yourname=raw_input()
flag=NOTFOUND()
for i in range(0,6,1):
a=cmp(masterlist[i],yourname)
if a==0:
print "Welcome,you can enter the palace\n"
flag=FOUND()
break
if flag==NOTFOUND():
print "Sorry,you are a trespasser\n"

Enter your name
gopal
Welcome,you can enter the palace



## EXAMPLE ON PAGE:327¶

In [46]:

names=["akshay","parag","raman","srinivas","gopal","rajesh"]
names = map(bytearray, names)
print "Original:%s %s\n" % (names[2],names[3])
s = sorted((names[2], names[3]), key=len)
for i in range(len(s[1])):
try:
t=s[0][i]
s[0][i]=s[1][i]
s[1][i]=t
except IndexError:
for _ in range(i, len(s[1])):
#remove the items from the longer string to and append them to the shorter one.
s[0].append(s[1].pop(i))
print "New:%s %s\n" % (names[2],names[3])

Original:raman srinivas

New:srinivas raman



## EXAMPLE ON PAGE-328¶

In [48]:

names=["akshay","parag","raman","srinivas","gopal","rajesh"]
print "Original:%s %s\n" % (names[2],names[3])
t=names[2]
names[2]=names[3]
names[3]=t
print "New:%s %s\n" % (names[2],names[3])

Original:raman srinivas

New:srinivas raman



## EXAMPLE ON PAGE:329¶

In [50]:

names=[]
for i in range(6):
names.append("\0")
for i in range(0,6,1):
print "Enter name"
names[i]=raw_input()

Enter name
RAM
Enter name
GOPAL
Enter name
PANDEY
Enter name
GOPU
Enter name
CHOTU
Enter name


## EXAMPLE ON PAGE:329-330¶

In [7]:

names=[]
for i in range(0,6):
names.append('\0')
for i in range(0,6,1):
print "Enter name"
n=raw_input()
length=len(n)
import copy
p=copy.copy(n)
names[i]=p
for i in range(0,6,1):
print "%s\n" % (names[i])

Enter name
MUKUT
Enter name
BIHARI
Enter name
PANDEY
Enter name
Enter name
PANDEY
Enter name
SUNITA
MUKUT

BIHARI

PANDEY