Worksheet exercise on strings:
1. >>>len(“Computer Science”) ………
2. Predict the following:
>>>str=”Pithon”
(a) >>>str[-2] (b) >>>str[0]+str[-1] (c) >>> str[10] (d) >>>str[1]=’y’
3. s=”Python World”
(a) s.index(‘y’) (b) s.index(‘y’,0) (c) s.index(‘ ‘, 2 , 8) (d) s.index(‘l’ , 3, 10)
4. str1=”twinkle twinkle little star”
(a) str1.find(‘twinkle’) (b) str1.find(‘twinkle’ , 2) (c) str1.find(‘twinkle’ , 9, 13)
5. Find :
(a) >>>”xyz”==”xyz” (b) >>>”xyz”==””wxyz” (c) “xyz” > “XYZ” (d) “XYZ” != “wxyz”
6. Find :
>>>S=”python program” ; P=”012345”
(a) S[5] (b) S[5:] (c) S[:5] (d) S[0:5] (e) S[ : : 2]
(f)S[: 10 : 2] (g) S[ : : -1] (h) S[-1 : -10 : -2] (i) S[ 8: -3 : 3] (j) S[-3 : 8 : 1]
(k) print(S[0] , ‘-‘ , S[0:5] , ‘*’ , S[-1:-8:-2]) (l) P[0:5]+’$’ + S[-1:0:2]
Membership operators:
Membership operators are two. They are ‘in’ and ‘not in’
‘in’ – returns True if a character or a string if exist in given string; False if not found
‘not in’ – returns True if a character or a string if does not exist in given string; False otherwise
7. ch=“Chennai Training Institute for Education”
(a) “chennai” in ch (b) “Ch” in ch (c) “Tute” not in ch
8. write a python code to reverse the given string:
9. Write the output of the python code:
(a) (b)
S=’computer’ S=’computer’
R=’’ R=’’
for i in S: for i in S:
R=R+i R= i + R
print(S+R) print(S+R)
(c) (d)
(a) 1.
S=’computer’ S=’computer’
R=’#’ R=’#’
for i in S: for i in S:
R= R + i R= I + R
print(S+R) print(S+R)