MENU

Chapter 12 Strings Solutions

Question - 21 : -
Describe isdecimal( ) with example.

Answer - 21 : -

The method isdecimal( ) checks whether the string consists of only decimal characters. This method is present only on Unicode objects.
Note : To define a string as Unicode, one simply prefixes a ‘u’ to the opening quotation mark of the assignment.
Below is the example.
Syntax :
Following is the syntax for isdecimal( ) method :
str.isdecimal( )

Question - 22 : -
Describe the following method trans.late(table, deletechars=””)

Answer - 22 : -

The method translate( ) returns a copy of the string in which all characters have been translated using table (constructed with the maketrans( ) function in the string module), optionally deleting all characters found in the string deletechars.

Question - 23 : -
Give an example of title( ) in Python

Answer - 23 : -

The following example shows the usage of title( ) method
# !/usr/bin/python
str = “this is string example….wow!!!”;
print str.title( );
On compile and run the above program, this will produce the following result :
This Is String Example….Wow!!!

Question - 24 : -
Define strip ([chars]) with its syntax

Answer - 24 : -

The method strip( ) returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters).
Syntax: str.strip([chars]);

Question - 25 : -
Explain Parameters of str.rjust(width[, fillchar])

Answer - 25 : -

width — This is the string length in total after padding.
fillchar — This is the filler character, default is a space.

Question - 26 : -
 Write the output of the given Python code # !/usr/bin/python
str = “this is really a string example…. wow!!!”;
str = “is”;
print str.rfind(str);
print str.rfind(str, 0,10);
print str.rfind(str, 10, 0);
print str.find(str);
print str.find(str, 0,10);
print str.find(str, 10, 0);

Answer - 26 : -

Above code will produce the following result :
5
5
-1
2
2
-1

Question - 27 : -
Write the output of the given code #!/usr/bin/python
str = “this-is-real-string-example….wow!!!”;
print “Min character: ” + min(str);
str = “this-is-a-string-example….wow!!!”;
print “Min character: ” + min(str);

Answer - 27 : -

Min character: !
Min character: !

Question - 28 : -
Write the output of the given code #!/usr/bin/python
str = “this is really a string example….wow!!!”;
print “Max character: ” + max(str);
str = “this is a string example….wow!!!”;
print “Max character: ” + max(str);

Answer - 28 : -

Output
Max character: y
Max character: x

Question - 29 : -
Study the given script
defmetasearch( ):
import re
p=re.compile(‘sing+’)
searchl=re.search(p,’ Some singers sing well’)
if searchl:
match=searchl.group( )
index=searchl.start( )
lindex=search 1 ,end( )
print “matched”, match, “at index”, index ,”ending at”, lindex
else:
print “No match found”
metasearch( )
What will be the output of the above script if search( ) from the re module is replaced by match ( ) of the re module. Justify your answer

Answer - 29 : -

The output would be “N match found”
Justification : re.search( ) rill attempt the pattern throughout the string, i ntil it finds a match. re.match( ) on the other hand, only attempts the pattern at the very start of the string.
Example :
>>>re.match(“d”, “abcdef’) # No match
>>>re.search(“d”, “abcdef’) # Match

Question - 30 : -
What is the concept of immutable strings ?

Answer - 30 : -

Strings are immutable means that the contents of string cannot be chrnged after it is created.
For example :
>>> str = ‘Meney’
>>> str [3] = ‘h’
Type Error : ‘str’ object not support item assignment Python does not allow to change a character in a string. So an attempt to replace ‘e’ in the string by ‘h’ displays a Type Error.

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