Chapter 7 : Strings and things

example 7.1 page no : 69

In [1]:
'''
example 7.1 page no : 69
'''

fruit = "banana"
index = 0
while(index < len(fruit)):
    letter = fruit[index]
    print letter
    index = index + 1
b
a
n
a
n
a

example 7.2 page no :71

In [2]:
'''
example 7.2 page no :71
'''

def find(s,c,i):
    while (i<len(s)):
        if (s[i] == c): return i
        i = i + 1
    return -1;
    
print find("banana",'b',0)
print find("banana",'b',3)
0
-1

example 7.3 page no : 71

In [3]:
'''
example 7.3 page no : 71
'''

fruit = "banana"
length = len(fruit)
count = 0
index = 0
while (index < length):
    if (fruit[index] == 'a'):
        count = count + 1
    index = index + 1
print count 
3

example 7.4 page no : 72

In [4]:
'''
example 7.4 page no : 72
'''

fruit = "banana";
bakedGood = " nut bread";
dessert = fruit + bakedGood;
print dessert
banana nut bread

example 7.5 page no :73

In [5]:
'''
example 7.5 page no :73
'''

suffix = "ack"
letter = 'J'
while (letter <= 'Q'):
    print letter + suffix 
    c = ord(letter) + 1
    letter = chr(c)
Jack
Kack
Lack
Mack
Nack
Oack
Pack
Qack

example 7.6 page no :74

In [1]:
'''
example 7.6 page no :74
'''
word = "Zebra"

if (word < "banana"):
    print "Your word, " , word , ", comes before banana."
elif (word > "banana"):
    print "Your word, " , word , ", comes after banana." 
else:
    print "Yes, we have no bananas!" 
Your word,  Zebra , comes before banana.

Example 7.7 page no : 75

In [2]:
a = 'a'
print a.upper()
A
In [ ]: