Chapter 6: Applications with Strings and Text

Program 6.1, page no. 221

In [1]:
print "The character \\0 is used to terminate a string."
The character \0 is used to terminate a string.

Program 6.2, page no. 223

In [2]:
str1 = "To be or not to be"
str2 = ",that is a question"

print "Length of the string \"", str1, "\" is ", len(str1), " characters"
print "Length of the string \"", str2, "\" is ", len(str2), " characters"
Length of the string " To be or not to be " is  18  characters
Length of the string " ,that is a question " is  19  characters

Program 6.3, page no. 225

In [3]:
str1 = "Computers do what you tell them to do, not what you want them to do."
str2 = "When you put something in memory, remember where you put it."
str3 = "Never test for a condition you don't know what to do with."

print "There are 3 strings."
print "The string: "
print str1
print "contains ", len(str1), " characters"
print "The string: "
print str2
print "contains ", len(str3), " characters"
print "The string: "
print str3
print "contains ", len(str3), " characters"
There are 3 strings.
The string: 
Computers do what you tell them to do, not what you want them to do.
contains  68  characters
The string: 
When you put something in memory, remember where you put it.
contains  58  characters
The string: 
Never test for a condition you don't know what to do with.
contains  58  characters

Program 6.4, page no. 230

In [4]:
preamble = "The joke is: "
str1 = "My dog hasn\'t got any nose.\n"
str2 = "How does your dog smell then?\n"
str3 = "My dog smells horrible.\n"
joke = str1 + str2 + str3

print preamble
print joke
The joke is: 
My dog hasn't got any nose.
How does your dog smell then?
My dog smells horrible.

Program 6.5, page no. 234

In [ ]:
print "Type in the first word (maximum 20 characters): ",
word1 = raw_input()
print "Type in the second word (maximum 20 characters): ",
word2 = raw_input()

if(word1 == word2):
    print "You have entered identical words"
else:
    print "%s precedes %s\n" %((word2 if(word1 > word2) else word1), (word1 if(word1 > word2) else word2)) 

Program 6.6, page no. 240

In [6]:
str1 = "This string contains the holy grail."
str2 = "the holy grail"
str3 = "the holy grill"
 
if str2 in str1:
    print "\"%s\" was found in \"%s\"\n" %(str2, str1)
else:
    print "\n\"%s\" was not found." %str2
 
if not(str3 in str1):
    print "\"%s\" was not found.\n" % str3
else:
    print "\nWe shouldn't get to here!"
"the holy grail" was found in "This string contains the holy grail."

"the holy grill" was not found.

Program 6.7, page no. 243

In [9]:
print "Enter some prose that is less than 1000 characters (go on typing hit enter to terminate):"
str1 = raw_input()

if len(str1) > 1000:
    print "Maximum permitted input length exceeded."

list_str1 = str1.split(" ")

print "The words in the prose that you entered are: "

for i in range(0, len(list_str1)):
    print list_str1[i], "\t",
    if i % 5 == 0:
        print "\n"

print "Words found ", len(list_str1)
Enter some prose that is less than 1000 characters (go on typing hit enter to terminate):
y father's family name being Pirrip, and my Christian name Philip, my infant tongue could make of both names nothing longer or more explicit than Pip. So, I called myself Pip, and came to be called Pip.
The words in the prose that you entered are: 
y 	

father's 	family 	name 	being 	Pirrip, 	

and 	my 	Christian 	name 	Philip, 	

my 	infant 	tongue 	could 	make 	

of 	both 	names 	nothing 	longer 	

or 	more 	explicit 	than 	Pip. 	

So, 	I 	called 	myself 	Pip, 	

and 	came 	to 	be 	called 	

Pip. 	Words found  37

Program 6.8, page no. 249

In [10]:
nLetters = 0
nDigits = 0
nPunct = 0
print "Enter an interesting string of less than 100 characters: "
str1 = raw_input()

for char in str1:
    if char.isalpha():
        nLetters += 1
    elif char.isdigit():
        nDigits += 1
    elif char == " ":
        pass
    else:
        nPunct += 1

print "Your string contained %d letters, %d digits and %d punctuation characters. " %(nLetters, nDigits, nPunct)
Enter an interesting string of less than 100 characters: 
I was born on the 3rd of October 1895, which is long ago.
Your string contained 38 letters, 5 digits and 2 punctuation characters. 

Program 6.9, page no. 251

In [11]:
print "Enter the string to be searched (less than 100 characters): ",
text = raw_input()
 
print "Enter the string sought (less than 40 characters):",
substring = raw_input()
 
print "First string entered: ", text
print "Second string entered: ", substring
 
text = text.upper()
substring = substring.upper() 
print "The second string %s found in the first. " %("was not" if not substring in text else "was")
Enter the string to be searched (less than 100 characters): Cry havoc, and let slip the dogs of war.
 Enter the string sought (less than 40 characters):The Dogs of War
 First string entered:  Cry havoc, and let slip the dogs of war.
Second string entered:  The Dogs of War
The second string was found in the first. 

Program 6.10, page no. 259

In [21]:
import numpy

print "Enter text (hit enter to exit): ",
text = raw_input()

text_split = text.split()
distinct_words = []

for i in text_split:
    if i not in distinct_words:
        distinct_words.append(i)

word_count = numpy.zeros(len(distinct_words))

for i in range(len(distinct_words)):
    for j in range(len(text_split)):
        if distinct_words[i] == text_split[j]:
            word_count[i] += 1

for i in range(len(distinct_words)):
    print distinct_words[i], int(word_count[i])
    if i == 5:
        print ""
Enter text (hit enter to exit): When I makes tea I makes tea, as old mother Grogan said. And when I makes water I makes water. Begob, ma'am, says Mrs Cahill, God send you don't make them in the same pot.
 When 1
I 4
makes 4
tea 1
tea, 1
as 1

old 1
mother 1
Grogan 1
said. 1
And 1
when 1
water 1
water. 1
Begob, 1
ma'am, 1
says 1
Mrs 1
Cahill, 1
God 1
send 1
you 1
don't 1
make 1
them 1
in 1
the 1
same 1
pot. 1