Page 326 - Computer Science Class 11 With Functions
P. 326
10. For performing each of the following tasks, suggest the name of a suitable str method,
(i) To check whether the string contains whitespace characters.
(ii) To get a string by replacing the lowercase alphabets of the given string to uppercase.
(iii) To get a string by removing all leading and trailing whitespace characters from a string.
(iv) To check whether a string occurs as a substring of a given string.
(v) To check whether the characters of a string are in lowercase.
Ans. (i) isspace()
(ii) upper()
(iii) strip()
(iv) find()
(v) islower()
11. Identify the errors (if any) in the given code:
message1 = "Good'
message2 = "Night"
num = 5
print(message1 + message2)
print(message1 * message2)
print(message1 + num)
print(message2 * num)
Ans. message1 = "Good’ —-> double quotes on both sides of string
message2 = "Night"
num=5
print(message1 + message2)
print(message1 * message2) —> two strings cannot be multiplied
print(message1 + num) —> A string cannot be concatenated with a number
print(message2 * num)
12. Write a function in Python that returns a string, replacing every alphabetic character at even index (index 0, 2, 4, etc.) with
the corresponding uppercase letter. For example, if the string is "Welcome all," the output will be "WeLcOmE AlL".
Ans. def alter(txt):
'''
Objective: .
Input Parameter:a string
Return Value: a string that replaces alphabetic characters at even indices with
corresponding uppercase letters.
'''
length = len(txt)
finalTxt = ""
for i in range(0, length):
if i%2 == 0:
finalTxt += txt[i].upper()
else:
finalTxt += txt[i]
#if i<(length-1):
# finalTxt += txt[i+1].upper()
return finalTxt
324 Touchpad Computer Science-XI

