Exercises · Q2
Q.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())
ii. print(myAddress.upper())
iii. print(myAddress.count('New'))
iv. print(myAddress.find('New'))
v. print(myAddress.rfind('New'))
vi. print(myAddress.split(','))
vii. print(myAddress.split(' '))
viii. print(myAddress.replace('New','Old'))
ix. print(myAddress.partition(','))
x. print(myAddress.index('Agra'))
Tamil Nadu DgeTextbookSubjective· 4mImportance★★★★★est
62% · 8/13 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Ten string-method calls on myAddress = "WZ-1,New Ganga Nagar,New Delhi" — the highlights: "New" occurs twice (indices 5 and 21), split() results differ by separator, and index('Agra') raises a ValueError because unlike find(), index() does not return -1.
First map the positions that matter. Counting characters of "WZ-1,New Ganga Nagar,New Delhi":
W(0) Z(1) -(2) 1(3) ,(4) N(5) e(6) w(7) ' '(8) G(9)...r(19) ,(20) N(21) e(22) w(23) ' '(24) D(25)...i(29)
So the substring "New" starts at index 5 (first) and index 21 (second). Now each statement:
| # | Statement | How it evaluates | Output |
|---|---|---|---|
| i | myAddress.lower() | all letters to lowercase | wz-1,new ganga nagar,new delhi |
| ii | myAddress.upper() | all letters to uppercase | WZ-1,NEW GANGA NAGAR,NEW DELHI |
| iii | myAddress.count('New') | non-overlapping occurrences of "New" | 2 |
| iv | myAddress.find('New') | index of the FIRST occurrence | 5 |
| v | myAddress.rfind('New') | index of the LAST occurrence (search from right) | 21 |
| vi | myAddress.split(',') | cut at every comma → list of 3 parts | ['WZ-1', 'New Ganga Nagar', 'New Delhi'] |
| vii | myAddress.split(' ') | cut at every space — commas stay glued to words | ['WZ-1,New', 'Ganga', 'Nagar,New', 'Delhi'] |
| viii | myAddress.replace('New','Old') | replaces BOTH occurrences | WZ-1,Old Ganga Nagar,Old Delhi |
| ix | myAddress.partition(',') | splits at the FIRST comma only, into a 3-tuple (before, separator, after) | ('WZ-1', ',', 'New Ganga Nagar,New Delhi') |
| x | myAddress.index('Agra') | "Agra" does not occur; index() raises an exception instead of returning -1 | ValueError: substring not found |
Complete run:
wz-1,new ganga nagar,new delhi
WZ-1,NEW GANGA NAGAR,NEW DELHI
2
5
21
['WZ-1', 'New Ganga Nagar', 'New Delhi'] …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.