Chapter 5 : Strings

Example 5.1, Page No. 97

In [6]:
print "Menu"
print "====="
print "A. Order"
print "B. Invoice"
print "C. Warehouse"
print "D. Finance"
cSel = raw_input("Select: ")
if cSel=='A' :
    print "You Selected Order"
elif cSel=='B' :
    print "You Selected Invoice"
elif cSel=='C':
    print "You Selected Warehouse"
elif cSel=='D' :
    print "You Selected Finance"
else :
    print "Erroneous Choice"
    
Menu
=====
A. Order
B. Invoice
C. Warehouse
D. Finance
Select: A
You Selected Order

Example 5.2, Page No.99

In [8]:
import sys
cSal = 'A'
while cSal!='X':
    print "Menu"
    print "====="
    print "A. Order"
    print "B. Invoice"
    print "C. Warehouse"
    print "D. Finance"
    print "X. Exit"
    cSel = raw_input("Select: ")

    
    if cSel=='A' :
        print "You Selected Order"
    elif cSel=='B' :
        print "You Selected Invoice"
    elif cSel=='C':
        print "You Selected Warehouse"
    elif cSel=='D' :
        print "You Selected Finance"
    elif cSel=='X':
        sys.exit()
    else :
        print "Erroneous Choice"
Menu
=====
A. Order
B. Invoice
C. Warehouse
D. Finance
X. Exit
Select: A
You Selected Order
Menu
=====
A. Order
B. Invoice
C. Warehouse
D. Finance
X. Exit
Select: B
You Selected Invoice
Menu
=====
A. Order
B. Invoice
C. Warehouse
D. Finance
X. Exit
Select: X
An exception has occurred, use %tb to see the full traceback.

SystemExit
To exit: use 'exit', 'quit', or Ctrl-D.

Example 5.3, Page No. 102

In [21]:
x = 'X' 
blank = ' '
for i in range(10, -1, -1):
    for j in range(i):
        print blank,
    print x,
    for j in range(10-i):
        print x, x,
    print ''
for i in range(2):
    for j in range(10):
        print blank,
    print x
                    X 
                  X X X 
                X X X X X 
              X X X X X X X 
            X X X X X X X X X 
          X X X X X X X X X X X 
        X X X X X X X X X X X X X 
      X X X X X X X X X X X X X X X 
    X X X X X X X X X X X X X X X X X 
  X X X X X X X X X X X X X X X X X X X 
X X X X X X X X X X X X X X X X X X X X X 
                    X
                    X

Example 5.4, Page No. 105

In [22]:
import sys
cName = raw_input("Enter your name: ")
print "Your name is " , cName
print "Your name is " , len(cName), "  Characters long"
print "Your entire string is " , sys.getsizeof(cName), " characters long"
Enter your name: John Smith
Your name is  John Smith
Your name is  10   Characters long
Your entire string is  31  characters long

Example 5.5, Page No. 106

In [17]:
cName=range(30)
cName = raw_input("Enter your name in lower case: ")
iLen = len(cName)
for ltr in cName:
    print ltr.lower(),
Enter your name in lower case: ABCD
a b c d

Example 5.6, Page No. 107

In [1]:
cName = raw_input("Enter your name: ")
iLen = len(cName)
cInt = []
cInt.append(cName[0])
for i in range(1, iLen):
    if cName[i] == ' ':
        cInt.append(cName[i+1])
print "Your Initials are ", ''.join(cInt)
Enter your name: Kjell Backman
Your Initials are  KB

Example 5.7, Page No. 109

In [33]:
cName1 = raw_input("Enter a Name: ")
cName2 = raw_input("Enter another Name: ")
if cName1 > cName2 :
    print cName1 , cName2
else :
    print cName2 , cName1
Enter a Name: abcd
Enter another Name: abcx
abcx abcd

Example 5.8, Page No. 110

In [7]:
print "Enter 5 names: "
names = []
for i in range(5):
    names.append(raw_input("name " + str(i+1) + ": "))
names.sort()
for i in range(5):
    print names[i]
Enter 5 names: 
name 1: hardik
name 2: Parth
name 3: Nikunj
name 4: Vaibhav
name 5: Chirag
Chirag
Nikunj
Parth
Vaibhav
hardik

Example 5.9, Page No.113

In [10]:
cName = raw_input("Enter Your Name: ")
for i in range(29):
    if cName[i] == ' ':
        break
cFirst=cName[0:i]
print cFirst
cSur=cName[i+1:]
print cSur
cSur=cSur+" "
cSur=cSur+cFirst
print cSur
cSur=cSur[::-1]
print cSur
Enter Your Name: John Smith
John
Smith
Smith John
nhoJ htimS

Example 5.10, Page No.115

In [24]:
cEncrypt = []
cName = []
cName = raw_input("Enter Your Name: ")
iLen = len(cName)
for i in range(iLen):
    asc=ord(cName[i])
    asc=asc+1
    cEncrypt.append(chr(asc))
print ''.join(cEncrypt)
Enter Your Name: JohnSmith
KpioTnjui

Example 5.11, Page No. 116

In [37]:
import random
iLen = random.randint(0,100)%3+5
cPw=[]
for i in range(iLen):
    val=random.randint(0,122)%26+65
    cPw.append(chr(val))
print ''.join(cPw)
ZXZUOY

Example 5.12, Page No. 119

In [3]:
cText=[]
cEncrypt=[]
cTemp1=[]
cText=raw_input("\nWrite a text in upper case: ")
iLen= len(cText)
for i in range(iLen):
    print cText[i]
    car=ord(cText[i])
    cEncrypt.append(car)
print ''.join(str(cEncrypt))
Write a text in upper case: hellow
h
e
l
l
o
w
[104, 101, 108, 108, 111, 119]
In [15]:
import random
cKey="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
cKey=list(cKey)
cEncrypt=range(50)
for i in range(26):
    j=random.randint(0,100)%26
    cTemp=cKey[i]
    cKey[i]=cKey[j]
    cKey[j]=cTemp
cKey="".join(map(str,cKey))
cText=raw_input("\nWrite a text in upper case: ")
iLen= len(cText)
for i in range(iLen):
    cEncrypt[i]=cKey[ord(cText[i])-65]
cEncrypt=cEncrypt[::iLen]
print "".join(map(str,cEncrypt))
Write a text in upper case: JOHNSMITH
R918273645
In [ ]: