Page 329 - Computer Science Class 11 With Functions
P. 329
Ans. To develop the desired function, Thomas needs to create a new string using the following approach:
a. Find length of the given word using the function len().
b. Intialise the desired toggle word (toggleWord) as null string.
c. for each character in the original string,
i. if it is placed at even index:
append it as it to toggleWord
ii. else:
Capitalize the character and append to toggleWord
Now the function can be developed as follows:
def toggleCase(word):
'''
Objective: To capitalize every alternate alphabet of a word
Input Parameters: word - string
Return Value: alternateCaseWord - string
'''
toggleWord = ''
for indx in range(len(word)):
if indx % 2 == 0:
toggleWord += word[indx]
else:
toggleWord += word[indx].upper()
return toggleWord
word = input("Enter a word : ")
toggleWord = toggleCase(word)
print("The new string is : ", toggleWord)
Assessment
A. Multiple Choice questions
1. Which of the following operators is not compatible with strings?
a. + b. * c. ** d. in
2. Which of the following statements will return the last two characters of the string, message?
a. message[0:2] b. message[-1:2] c. message[2:0] d. message[-1:-3:-1]
3. Given a string, named, sentence, which of the following statements will return a list of words?
a. sentence.split()
b. sentence.partition()
c. sentence.words()
d. sentence.substr()
Strings 327

