Skip to content
Exercises · Q1

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())

Punjab PsebTextbookSubjective· 4mImportance★★★★★est
54% · 7/13 Questions
✓ Free question

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:

#StatementHow it evaluatesOutput
iprint(mySubject[0:len(mySubject)])[0:16] — from start up to (not including) 16 → the entire stringComputer Science
iiprint(mySubject[-7:-1])-7 is index 9 ('S'), -1 is index 15 (excluded) → indices 9–14Scienc
iiiprint(mySubject[::2])every 2nd char: indices 0,2,4,6,8,10,12,14 → C,m,u,e,space,c,e,cCmue cec
ivprint(mySubject[len(mySubject)-1])mySubject[15] — the last charactere
vprint(2*mySubject)* repeats the string twice, no separatorComputer ScienceComputer Science
viprint(mySubject[::-2])step -2 starts at index 15: 15,13,11,9,7,5,3,1 → e,n,i,S,r,t,p,oeniSrtpo
viiprint(mySubject[:3] + mySubject[3:])"Com" + "puter Science" — a slice pair at the same cut always rebuilds the stringComputer Science
viiiprint(mySubject.swapcase())every uppercase becomes lowercase and vice versacOMPUTER sCIENCE
ixprint(mySubject.startswith('Comp'))the string does begin with "Comp"True
xprint(mySubject.isalpha())isalpha() is True only if ALL characters are letters — the space fails itFalse

Complete output, in order:

Computer Science
Scienc
Cmue cec
e
Computer ScienceComputer Science
eniSrtpo
Computer Science
cOMPUTER sCIENCE
True
False
Watch out

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.

✓Final answer

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.