Chapter 10: Strings

Example 10.3, Page Number: 10.3

In [4]:
st='Programming'
print 'The string is %s' %st
The string is Programming

Example 10.4, Page Number: 10.4

In [5]:
st='Hello World'
print 'The line is :'
print st
The line is :
Hello World

Example 10.6, Page Number: 10.5

In [6]:
strin='Welcome to python'
for i in strin:
    print i,
W e l c o m e   t o   p y t h o n

Example 10.7, Page Number: 10.6

In [7]:
str1='Programming'
len1=len(str1)
print 'The length of the string is ',len1

str2=str1
print 'First string is %s and copied string is %s' %(str1,str2)

str3='Computer'

if str1==str3:
    print 'Both strings are equal'
elif str1<str2:
    print 'First string is lesser than second string'
else:
    print 'First string is greater than second string'
    
tempstr=' with C'
str1=str1+tempstr
print 'The concated string is ',str1
The length of the string is  11
First string is Programming and copied string is Programming
First string is greater than second string
The concated string is  Programming with C

Example 10.8,Page Number: 10.7

In [8]:
def strlength(str1):
    count=0
    for i in str1:
        count+=1
    
    return count
def strcopy(src):
    dst=[]
    for i in src:
        dst.append(i)
        
    dst=''.join(dst)
        
    return dst


str1='New Delhi'
len1=strlength(str1)
print 'The length of the string is ',len1

str2=strcopy(str1)
print 'First string is %s and copied string is %s' %(str1,str2)
The length of the string is  9
First string is New Delhi and copied string is New Delhi

Example 10.9,Page Number: 10.9

In [9]:
def strcompare(str1,str2):
    
    len1=len(str1)
    len2=len(str2)
    
    if len1<len2:
        length=len1
    else:
        length=len2
        
    for i in xrange(0,length):
        if str1[i]<str2[i]:
            return -1
        elif str1[i]>str2[i]:
            return 1
    
    return 0

str1='Programming'
str2='Computer'
status=strcompare(str1,str2)
if status==-1:
    print 'First string is lesser than second string'
elif status==1:
    print 'First string is greater than second string'
else:
    print 'Both strings ae equal'
First string is greater than second string

Example 10.10, Page Number: 10.10

In [10]:
def leftconcat(dst,src):
    dst=src+dst
    return dst

def rightconcat(dst,src):
    dst=dst+src
    return dst

str1='Hello'
str2='Friends'

tempstr=leftconcat(str2,str1)
print 'The first string after left concatenation becomes ', tempstr

tempstr=rightconcat(str2,str1)
print 'The first string after right concatenation becomes', tempstr
The first string after left concatenation becomes  HelloFriends
The first string after right concatenation becomes FriendsHello

Example 10.11,Page Numbr: 10.12

In [11]:
str1='All good boys have bread'
count=0

for i in str1:
    if i=='a' or i=='e'or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U' :
        count+=1
        
print 'Total number of vowels in a given text are ',count
Total number of vowels in a given text are  8

Example 10.12, Page Number: 10.13

In [12]:
ch1='A'
ch2=ord(ch1)+3
print chr(ch2)

ch1=chr(ord(ch1)+1)
print ch1

print ord('a')
print ord('l')

val=ord(ch1)*ch2
print val

print chr(100)
D
B
97
108
4488
d

Example 10.13, Page Number: 10.13

In [13]:
text='I am studying 6 Theory Papers & 4 practicals'

len1=len(text)
text=list(text)
for i in xrange(0,len1):
    if text[i]>='a' and text[i]<='z':
        text[i]=chr(ord(text[i])+ord('A')-ord('a'))
        
    
text=''.join(text)

print 'The text after converting lowercase alphabets to uppercase is '
print text
The text after converting lowercase alphabets to uppercase is 
I AM STUDYING 6 THEORY PAPERS & 4 PRACTICALS

Example 10.14, Page Number: 10.14

In [14]:
text='The programming is a systematic process'
substr='pro'
text_len=len(text)
sub_len=len(substr)
text=list(text)
substr=list(substr)

for i in xrange(0,text_len-sub_len+1):
    
    for j in xrange(0,sub_len):
        
        if text[i+j]==substr[j]:
            continue
        else:
            break
        
    if j==sub_len-1:
        print 'The substring is present from subscript %d onwards' %i
            
The substring is present from subscript 4 onwards
The substring is present from subscript 32 onwards

Example 10.15,Page number: 10.15

In [15]:
def reorder(x):

    n=len(x)
    for item in range(0,n-1):
        for i in range(item+1,n):
            if x[item]>x[i]:
                temp=x[item]
                x[item]=x[i]
                x[i]=temp


    return

x=['PACIFIC','ATLANTIC','INDIAN','CARIBBEAN','BERING','BLACK','RED','NORTH','BALTIC','CASPIAN']
print 'Original list of strings :\n\n'

for i in x:
    print "String : ",i

reorder(x)

print "\nReodered list of strings : \n\n"

for i in x:
    print "String : ",i
Original list of strings :


String :  PACIFIC
String :  ATLANTIC
String :  INDIAN
String :  CARIBBEAN
String :  BERING
String :  BLACK
String :  RED
String :  NORTH
String :  BALTIC
String :  CASPIAN

Reodered list of strings : 


String :  ATLANTIC
String :  BALTIC
String :  BERING
String :  BLACK
String :  CARIBBEAN
String :  CASPIAN
String :  INDIAN
String :  NORTH
String :  PACIFIC
String :  RED
In [15]: