Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
76 views6 pages

String Worksheet With Answer

The document is a revision guide for Class XII Computer Science focusing on string manipulations in Python. It includes objective-based questions, predicted outputs, and programming tasks related to string operations. Key topics covered include string methods, error identification, and practical programming exercises.

Uploaded by

ydoddaga
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
76 views6 pages

String Worksheet With Answer

The document is a revision guide for Class XII Computer Science focusing on string manipulations in Python. It includes objective-based questions, predicted outputs, and programming tasks related to string operations. Key topics covered include string methods, error identification, and practical programming exercises.

Uploaded by

ydoddaga
Copyright
© © All Rights Reserved
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/ 6

VELAMMAL VIDYALAYA, PARUTHIPATTU

CLASS: XII
SUBJECT: COMPUTER SCIENCE (083)
CHAPTER: PYTHON REVISION TOUR – II
TOPIC: STRING MANIPULATIONS

I. OBJECTIVE BASED QUESTIONS:


1. State True or False:
A string can be surrounded by three sets of single quotation marks or by three sets of double
quotation marks.
Ans:True
2. What error occurs when you execute the following statement?
apple = mango
a) SyntaxError b) NameError c) ValueError d) TypeError
Ans: b) NameError
3. Which out of the following operators will NOT work with a string?
+ , -, *, not, in
Ans: -, not
4. If a = ‘8’, what will be the result of a*2 + str(int(a)*2)
a) ‘32’ b) ‘8816’ c) ‘8888’ d) Error
Ans: b) ‘8816’
5. Assertion (A): The expression "Hello".sort() in Python will give an error.
Reason (R): sort() does not exist as a method/function for strings in Python.
Mark the correct choice as
(a) Both (A) and (R) are true and (R) is the correct explanation for (A).
(b) Both (A) and (R) are true and (R) is not the correct explanation for (A).
(c) (A) is true but (R) is false. (d) (A) is false but(R) is true.
Ans: (a)
6. Write a statement to display the number of occurrences of the substring "is" in a string
named message.
For ex, if the string message contains "This is his book", then the output will be 3.
Ans: message.count(‘is’)
7. What does the replace(‘e’,’h’,1) method of string does?
i) Replaces the first occurrence of ‘e’ to ‘h’ ii) Replaces the first occurrence of ‘h’ to ‘e’
iii) Replace all occurrences of ‘e’ to ‘h’ iv) Replaces all occurrences of ‘h’ to ‘e’
Ans: i) Replaces the first occurrence of ‘e’ to ‘h’
8. Identify the statement from the following which will raise an error:
(a) print("A"*3) (b) print(5*3) (c) print("15" + 3) (d) print("15" + "13")
Ans: (c) print("15" + 3)
9. Write the Python statement to replace the string "This" with "That" in the string str1
using BUILT-IN functions/methods only.
Ans: str1.replace(‘This’,’That’)
10. Write the Python statement to display words in a string S in the form of a list.
Ans: S.split()
11. Write a Python statement to display alternate characters of a string, named
my_exam. For example, if my_exam="Russia Ukraine"
The statement should display Rsi kan
Ans: my_exam[::2] or any valid statement
II. PREDICT THE OUTPUT:
12. str1= “6/4”
print(“str1”)
Ans: str1
13. text = "PYTHONPROGRAM"
text=text.replace('PY','#')
print(text)
Ans: #THONPROGRAM
14. >>> test=”Hello World”
>>> test[6]='t'
Ans: TypeError
15. print(“Viksit BHARAT"[-2::-2].capitalize())
print("I,Love India".capitalize())
print("I,Love INDIA".title())
Ans: Aabtsi
I,love india
I,Love India
16. a="Year 2024 at All the best".split("2")
a=a[0] + ". " + a[1] + ". " + a[2]
print (a)
Ans: Year . 0. 4 at All the best
17. Str=“Computer” [-4:]
print(Str*2)
Ans: uteruter
18. str="R and Data Science"
z=str.split()
newstr="=".join([z[2].upper(),z[3],z[2]+z[3],z[1].capitalize()])
print(newstr)
Ans: 'DATA=Science=DataScience=And'
19. s ="Question paper 2022-23"
s= s.split('2')
print(s)
Ans: ['Question paper ', '0', '', '-', '3']
20. a = "assistance"
a = a.partition('a')
b = a[0] + "-" + a[1] + "-" + a[2]
print (b)
Ans: -a-ssistance
21. >>> Str= "BHASHA SANGAM @ 75"
>>> S=Str.partition(" ")
>>> print(S)
Ans: ('BHASHA', ' ', 'SANGAM @ 75')
22. (i) S= "Amrit Mahotsav @ 75" (ii) S=”Computer Science” (iii) s=”Python”
A=S.partition (" ") print(S.partition(“W”)) print(s.partition(‘P’))
print (a)
Ans: NameError
23. (i)print("World Peace" [-2::-2])
ce lo
(ii) print("#G20 Presidency"[-2:2:-2])
ceieP0
(iii) print("World Cup 2025”[-6::-1])
puC dlroW
(iv) print("Olympics 2024" [-2::-4])
2sm
(v) print(‘Welcome to Python world‘[: : -3])
dont oe
(vi) print("COMPUTER SCIENCE"[:12:-2])
EN
(vii) print(“Python”[6:14])
Blankline
24. myStr = "MISSISSIPPI"
print(myStr[:4]+"#"+myStr[–5:])
Ans: MISS#SIPPI
25. event="G20 Presidency@2023"
L=event.split(' ')
print(L[::–2])
Ans: ['Presidency@2023']
26. remark = "SQL - Structured Query Language"
note = remark[2:18].split()
print(note)
Ans: ['L', '-', 'Structured', 'Q']
27. for i in "QUITE":
print([i.lower0], end="#")
Ans: ['q']#['u']#['i']#['t']#['e']#
28. str1=" Programming "
str2=" is My Junoon"
str3=str1.strip()+str2.replace("J", "j").lstrip()
print(str3.partition("My"))
Ans: ('Programmingis ', 'My', ' junoon')
29. print("xyyzxyzxzxyy".count('xyy'))
print("xyyzxyzxzxyy".count('xy',1,10))
print(“Python”.find(‘n’))
print(“Python”.find(‘n’,2,5))
print(“Python”.find("h",-6,-1)
print(“Python”.find("h",-1,-6)
print(“Python”.find("h",-1,-6,-1)
Ans:
2
1
5
-1
3
-1
TypeError
30. >>>“Python”*-3
>>>”python”*0
>>>”python”*3.0
Ans:
“”
“”
TypeError
31.

Ans:John#1234#Vicky Ans: TypeError


32.

(iv) n = "Post on Twitter is trending" (v) “TEST”.split(‘T’,1)


q = n.split('t')
print( q[0]+q[2]+q[3]+q[4][1:] )
(vi) s=”TEST” (vii) country='International'
print(s.split(“t”)) print(country.split("n"))
(viii) S1= “India is on the Moon”
A=S1.split("o",3)
print(A)

Ans:
(i) [‘Commi’,’ment’]
(ii) [“”,” “,””]
(iii)[“”,””,””]
(iv) Poser is ending
(v) ['', 'EST']
(vi) ['TEST']
(vii) ['I', 'ter', 'atio', 'al']
(viii) ['India is ', 'n the M', '', 'n']
33. myStr = "MISSISSIPPI" S=”Birds feather”
print(myStr.index('S')) print(S.lstrip(“Brill”))
Ans : 2 Ans: ds feather
print(myStr.index('g')) print(S.rstrip(“Brill”))
Ans: ValueError: Ans: Birds feathe
print(myStr.index('S',3,15)) print(S.strip(“Brill”))
Ans: 3 Ans: ds feathe

34. (i) (ii)

Ans: **dAys Ans: fUN#pYTHONn#.


(iii) (iv)

Ans: R*A*C*E*C*A*R* Ans: i@DIA@gGroI@G


C#a#r#
R*A*D*A*R*

(v) (vi)

Ans: thon
5 Ans: ILENCE-^OPE-^UCCEs^^^^^
(vii)
Ans: bbcc
4
PROGRAM BASED QUESTIONS:
35. Write program in Python to input a line and display the occurrence of the word 'are'.
For example, if the line is:
Books are referred to as a man’s best friend. They are very beneficial for
mankind and have helped it evolve. Books leave a deep impact on us and are
responsible for uplifting our mood.
The output should be 3.
36. Write a Program to Count the Number of Vowels in a String.
37. Write a program to remove vowels from the original string using string function.
38. Write a program to find and display all the words longer than 5 characters in a string.
39. Write a program to find the shortest word in a line.
40. Write a program in python to input the string where every Nth alphabet of the
word is replaced with an underscore ("_").
For example: if string contains the word "TELEVISION" and N is 3, then the m
function should return the string "TE_EV_SI_N". Likewise for the word
"TELEVISION" if N is 4, then the function should return "TEL_VIS_ON".

You might also like