The document presents exercises related to string operations in Python, using two example strings: 'mySubject' and 'myAddress'. It includes various operations such as slicing, case conversion, counting, and finding substrings, along with their expected outputs. The exercises aim to demonstrate the manipulation and analysis of strings in Python programming.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
19 views2 pages
CH 8 STRINGS TB Exercise
The document presents exercises related to string operations in Python, using two example strings: 'mySubject' and 'myAddress'. It includes various operations such as slicing, case conversion, counting, and finding substrings, along with their expected outputs. The exercises aim to demonstrate the manipulation and analysis of strings in Python programming.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
CHAPTER 8 STRINGS- TB EXERCISE
1. Consider the following string mySubject:
mySubject = "Computer Science" What will be the output of the following string operations : i. print(mySubject[0:len(mySubject)]) Ans: Computer Science ii. print(mySubject[-7:-1]) Ans: Scienc iii. print(mySubject[::2]) Ans: Cmue cec iv. print(mySubject[len(mySubject)-1]) Ans: e v. print(2*mySubject) Ans: Computer ScienceComputer Science vi. print(mySubject[::-2]) Ans: eniSrtpo vii. print(mySubject[:3] + mySubject[3:]) Ans: Computer Science viii. print(mySubject.swapcase()) Ans: cOMPUTER sCIENCE ix. print(mySubject.startswith('Comp')) Ans: True ix. print(mySubject.isalpha()) Ans:False 2. 2. Consider the following string myAddress: myAddress = "WZ-1,New Ganga Nagar,New Delhi" What will be the output of following string operations : I. print(myAddress.lower()) Ans: wz-1,new ganga nagar, new delhi ii. print(myAddress.upper()) Ans: WZ-1,NEW GANGA NAGAR, NEW DELHI iii. print(myAddress.count('New')) Ans: 2 iv. print(myAddress.find('New')) Ans:5 v. print(myAddress.rfind('New')) Ans: 21 vi. print(myAddress.split(',')) Ans: [‘WZ-1’,’New Ganga Nagar’,New Delhi’] vii. print(myAddress.split(' ')) Ans: [‘WZ-1,New’,’Ganga’,’Nagar,New’,’Delhi’] viii. print(myAddress.replace('New','Old')) Ans: WZ-1,Old Ganga Nagar,Old Delhi xi. print(myAddress.partition(',')) Ans:(‘WZ-1’, ’ , ‘ ,’New Ganga Nagar, New Delhi’) x. print(myAddress.index('Agra')) Ans: Error..Substring not found