Q.Consider the following string mySubject:
mySubject = "Computer Science"
What will be the output of the following string operations :
i. print(mySubject[0:len(mySubject)])
ii. print(mySubject[-7:-1])
iii. print(mySubject[::2])
iv. print(mySubject[len(mySubject)-1])
v. print(2*mySubject)
vi. print(mySubject[::-2])
vii. print(mySubject[:3] + mySubject[3:])
viii. print(mySubject.swapcase())
ix. print(mySubject.startswith('Comp'))
x. print(mySubject.isalpha())
Ten slicing/method operations on mySubject = "Computer Science" — the key facts are: len = 16, the space sits at index 8, slices exclude the stop index, and a negative step walks backwards.
Python strings are indexed from 0 (and from -1 backwards), and a slice s[start:stop:step] includes start but excludes stop. First map the string:
Index : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Char : C o m p u t e r S c i e n c e
Neg :-16-15-14-13-12-11-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
len(mySubject) = 16. Now trace each statement:
| # | Statement | How it evaluates | Output |
|---|---|---|---|
| i | print(mySubject[0:len(mySubject)]) | [0:16] — from start up to (not including) 16 → the entire string | Computer Science |
| ii | print(mySubject[-7:-1]) | -7 is index 9 ('S'), -1 is index 15 (excluded) → indices 9–14 | Scienc |
| iii | print(mySubject[::2]) | every 2nd char: indices 0,2,4,6,8,10,12,14 → C,m,u,e,space,c,e,c | Cmue cec |
| iv | print(mySubject[len(mySubject)-1]) | mySubject[15] — the last character | e |
| v | print(2*mySubject) | * repeats the string twice, no separator | Computer ScienceComputer Science |
| vi | print(mySubject[::-2]) | step -2 starts at index 15: 15,13,11,9,7,5,3,1 → e,n,i,S,r,t,p,o | eniSrtpo |
| vii | print(mySubject[:3] + mySubject[3:]) | "Com" + "puter Science" — a slice pair at the same cut always rebuilds the string | Computer Science |
| viii | print(mySubject.swapcase()) | every uppercase becomes lowercase and vice versa | cOMPUTER sCIENCE |
| ix | print(mySubject.startswith('Comp')) | the string does begin with "Comp" | True |
| x | print(mySubject.isalpha()) | isalpha() is True only if ALL characters are letters — the space fails it | False |
Complete output, in order:
Computer Science
Scienc
Cmue cec
e
Computer ScienceComputer Science
eniSrtpo
Computer Science
cOMPUTER sCIENCE
True
False
Two classic slips here: (ii) forgetting that the stop index -1 is excluded (so 'e' at the end is dropped, giving "Scienc" not "Science"), and (x) assuming isalpha() ignores spaces — it does not; one space makes it False.
i) Computer Science · ii) Scienc · iii) Cmue cec · iv) e · v) Computer ScienceComputer Science · vi) eniSrtpo · vii) Computer Science · viii) cOMPUTER sCIENCE · ix) True · x) False.
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.