MENU

Chapter 12 Strings Solutions

Question - 31 : -
Write a program to check whether the string is a palindrome or not.

Answer - 31 : -

def palindrom ( ) :
str = input (“Enter the string”)
l = len (str)
P = l – 1
inex = 0
while (index < p) :
if (str[index] = = str [p]):
index = index + 1
p = p-1
else :
print “String is not a palindrom” break
else :
print “String is a palindrom”

Question - 32 : -
Write a program to count number of ‘s’ in the string ‘successor’.

Answer - 32 : -

def letcount ( ) :
word = ‘successor’
count = 0
for letter in word :
if letter = = ‘s’ :
count = count + 1
print (count)

Question - 33 : -
Write a program to determine if the given word is present in the string.

Answer - 33 : -

def wsearch ( ) :
imprt re
word = ‘good’
search1 = re.search (word, ‘I am a good person’)
if search1 :
position = search1.start ( )
print “matched”, word, “at position”, position
else :
print “No match found”

Question - 34 : -
Input a string “Green Revolution”. Write a script to print the string in reverse.

Answer - 34 : -

def reverseorder(list 1) :
relist = [ ]
i = len (list 1) -1
while i > = 0 :
relist.append (list [i])
i = 1 -1
return relist

Question - 35 : -
Write a program to print the pyramid ?

Answer - 35 : -

num = eval (raw_input (“Enter an integer from 1 to 5:”))
if num < 6 :
for i in range (1, num + 1):
for j in range (num-i, 0,-1):
print (” “)
for j in range (i, 0, -1):
print (j)
for j in range (2, i+1):
print (j)
print (” “)
else :
print (“The number entered is greater than 5”)
Output :
 

Question - 36 : -
Write the output of the following python code #!/usr/bin/python
str = “Line1-a b c d e f\nLine2- a b
c\n\nLine4- a b c d”;
print str.splitlines( );
print str.splitlines(O);
print str.splitlines(3);
print str.splitlines(4);
print str.splitlines(5);

Answer - 36 : -

Output
[‘Linel-a b c d e f’, ‘Line2- a b c’, “, ‘Line4- abed’]
[‘Linel-a b c d e f’, ‘Line2- a b c’, “, ‘Line4- abed’]
[‘Linel-a b c d e f\ri, ‘Line2- a b c\ri, ‘\n’, ‘Line4- a b c d’]
[‘Linel-a b c d e f\n’, ‘Line2- a b c\ri, ‘\ri, ‘Line4- a b c d’]
[‘Linel-a b c d e f\ri, ‘Line2- a b c\ri, ‘\n’, ‘Line4- a bed’]

Question - 37 : -
Define split( ) with suitable example.

Answer - 37 : -

The method split( ) returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified), optionally limiting the number of splits to num.
Syntax
str.split(str=””, num=string.count(str)).
Parameters
str — This is any delimeter, by default it is space.
num — This is number of lines to be made.
Example
# !/usr/bin/python
str = “Linel-abcdef \nLine2-abc \nLine4-abcd”;
print str.split( ); print str.split(‘ 1 );
OUTPUT
[‘Linel-abcdef’, ‘Line2-abc’, ‘Line4-abcd’]
[‘Linel-abcdef’, ‘\nLine2-abc \nLine4-abcd’]

Question - 38 : -
Describe index(str, beg=0, end=len(string)) with example

Answer - 38 : -

The method index( ) determines if string str occurs in string or in a substring of string if starting indexbeg and ending index end are given. This method is same as find( ), but raises an exception if
sub is not found.
Syntax
str.index(str, beg=0 end=len(string))
Example
# !/usr/bin/python
str = “this is string example….wow!!!”;
str = “exam”;
print str.index(str);
print str.index(str, 10);
print str.index(str, 40);
OUTPUT
15
15
Traceback (most recent call last):
File “test.py”, line 8, in
print str.index(str, 40);
ValueError: substring not found
shell returned 1

Question - 39 : -
Write a program that reads a string and display the longest substring of the given string having just the consonants.

Answer - 39 : -

string = raw_input (“Enter a string :”)
length = len (string)
max length = 0
max sub = ‘ ‘
sub = ‘ ‘
lensub = 0
for a in range (length) :
if string [a] in aeiou ‘or string [a] in’AEIOU’:
if lensub > maxlength :
maxsub = sub
maxlength – lensub
sub = ‘ ‘
lensub 0
else :
sub += string[a]
lensub = len(sub)
a + = 1
print “Maximum length consonent
substring is :”, maxsub,
print “with”, maxlength, “characters”

Question - 40 : -
Consider the string str=”Global Warming” Write statements in Python to implement the following
(a) To display the last four characters.
(b) To display the substring starting from index 4 and ending at index 8.
(c) To check whether string has alphanu-meric characters or not
(d) To trim the last four characters from the string.
(e) To trim the first four characters from the string.
(f) To display the starting index for the substring „ WaD.
(g) To change the case of the given string.
(h) To check if the string is in title case.
(i) To replace all the occurrences of letter „aD in the string with „*?

Answer - 40 : -

(a) print str[-4:]
(b) print str[4:8]
(c) str.isalnum( )
(d) str[:-4]
(g) str.swapcase( )
(h) str.istitle( )
(i) str.replace(‘a’,’*’)
Output screenshot :
 

Free - Previous Years Question Papers
Any questions? Ask us!
×