Students can Download Computer Science Chapter 8 Strings and String Manipulations Questions and Answers, Notes Pdf, Samacheer Kalvi 12th Computer Science Book Solutions Guide Pdf helps you to revise the complete Tamilnadu State Board New Syllabus and score more marks in your examinations.
Tamilnadu Samacheer Kalvi 12th Computer Science Solutions Chapter 8 Strings and String Manipulations
Samacheer Kalvi 12th Computer Science Strings and String Manipulations Text Book Back Questions and Answers
PART – I
I. Choose The Best Answer
12th Computer Science Chapter 8 Book Back Answers Question 1.
Which of the following is the output of the following python code?
Answer:
str1=”TamilNadu”
print (str1 [:: -1])
(a) Tamilnadu
(b) Tmlau
(c) UdanlimaT
(d) UdaNlimaT
Answer:
(c) UdanlimaT
12th Computer Science Solution Book Question 2.
What will be the output of the following code?
Answer:
str1= “Chennai Schools”
str1[7] = “_”
(a) Chennai – Schools
(b) Chenna – School
(c) Type error
(d) Chennai
Answer:
(c) Type error
12th Computer Science Samacheer Kalvi Question 3.
Which of the following operator is used for concatenation?
(a) +
(b) &
(c) *
(d) =
Answer:
(a) +
Samacheer Kalvi Guru 12th Computer Science Question 4.
Defining strings within triple quotes allows creating:
(a) Single line Strings
(b) Multiline Strings
(c) Double line Strings
(d) Multiple Strings
Answer:
(b) Multiline Strings
Samacheer Kalvi 12th Computer Science Book Question 5.
Strings in python:
(a) Changeable
(b) Mutable
(c) Immutable
(d) Flexible
Answer:
(c) Immutable
Samacheer Kalvi 12th Computer Science Book Back Answers Question 6.
Which of the following is the slicing operator?
(a) { }
(b) [ ]
(c) <>
(d) ( )
Answer:
(b) [ ]
12th Computer Science Book Back Answers Question 7.
What is stride?
(a) Index value of slide operation
(b) First argument of slice operation
(c) Second argument of slice operation
(d) Third argument of slice operation
Answer:
(d) Third argument of slice operation
12th Computer Science Book Answers Question 8.
Which of the following formatting character is used to print exponential notation in upper case?
(a) % e
(b) % E
(c) % g
(d) % n
Answer:
(b) % E
Samacheer Kalvi Class 12 Computer Science Question 9.
Which of the following is used as placeholders or replacement fields which get replaced along with format ( ) function?
(a) { }
(b) <>
(c) ++
(d) ^^
Answer:
(a) { }
Samacheer Kalvi Computer Science Book Question 10.
The subscript of a string may be:
(a) Positive
(b) Negative
(c) Both (a) and (b)
(d) Either (a) or (b)
Answer:
(d) Either (a) or (b)
PART – II
II. Answer The Following Questions
Computer Science Samacheer Kalvi Question 1.
What is String?
Answer:
- String is a data type in python, which is used to handle an array of characters.
- String is a sequence of Unicode characters that may be a combination of letters, numbers, or special symbols enclosed within single, double, or even triple quotes.
Example:
‘Welcome to learning Python’
“Welcome to learning Python”
“”Welcome to learning Python””
12 Computer Science Book Back Answers Question 2.
Do you modify a string in Python?
Answer:
If you want to modify the string, a new string value can be assigned to the existing string variable. To define a new string value to the existing string variable. Python completely overwrites the new string on the existing string.
Example:
>>> str1=”How are you”
>>> print (str1)
How are you
>>> str1=”How about you”
>>> print (str1)
How about you
Samacheer Kalvi Guru 8th Computer Science Question 3.
How will you delete a string in Python?
Answer:
- Python will not allow deleting a particular character in a string.
- We can remove the entire string variable using a del command.
Example:
>>> strl=”How about you”
>>> print (str1)
How about you
>>> del str1
The str1 ‘How about you’ will be deleted
Class 12 Computer Science Book Back Answers Question 4.
What will be the output of the following python code?
Answer:
str1 = “School”
print (str1*3)
Output:
School School School
Question 5.
What is slicing?
Answer:
- A slice is a substring of the main string.
- A substring can be taken from the original string by using [ ] operator and index or subscript values.
- Thus, [ ] is also known as the slicing operator.
- Using the slice operator, we have to slice one or more substrings from the main string.
Example:
strl = «THIRUKKURAL»
(i) print (str1 [0])
T
(ii) print (str1 [0:5])
THIRU
PART – III
III. Answer The Following Questions
Question 1.
Write a Python program to display the given pattern?
Answer:
C O M P U T E R
C O M P U T E
C O M P U T
C O M P U
C O M P
C O M
C O
C
Program:
str1 = “COMPUTER”
index = len (str1)
for i in str 1:
print (str 1[: index])
index – = 1
Question 2.
Write a short about the following with a suitable example?
Answer:
Question 3.
What will be the output of the given python program?
str1 = “welcome”
str2 = “to school”
str3 = str1[: 2] str2[len(str2)-2:]
print (str3)
output:
Answer:
weoo 1
Question 4.
What is the use of format( )? Give an example?
Answer:
The formato function used with strings is very versatile and powerful function used for formatting strings.
The curly braces liare used as placeholders or replacement fields which get replaced along with format ()function.
Example:
numl=int (input(“Number 1: “))
num2=int (input(“Number 2: “))
print (“The, sum of {} and {} is {}”. format(numl,num2,(numl+num2)))
Output:
Number 1: 34
Number 2: 54
The sum of 34 and 54 is 88
Question 5.
Write a note about the count ( ) function in python?
Answer:
PART – IV
IV. Answer The Following Questions.
Question 1.
Explain string operators in python with suitable examples?
Answer:
- Python provides the following operators for string operations.
- These operators are useful to manipulate strings.
- They are
- Concatenation (+):
- Append (+ =)
- Repeating (*)
Concatenation (+):
- Joining of two or more strings is called as Concatenation. .
- The plus (+) operator is used to concatenate strings in python.
Example:
>>> “Welcome” + “Python”
WelcomePython
Append (+ =):
- Adding more strings at the end of an existing string is known as append. *
- The operator += is used to append a new string with an existing string.
Example:
>>> str1=”Welcome to”
>>> str1+=” Learn Python”
>>> print (str1)
Welcome to Learn Python
Repeating (*):
The multiplication operator (*) is used to display a string in multiple times.
Example:
>>> strl=”‘Welcome ”
>>> print (strl *4)
Welcome Welcome Welcome Welcome
Practice Programs
Question 1.
Write a python program to find the length of a string?
Answer:
str=input (“Enter a string: “)
print (len(str))
Output:
Enter a string: HELLO
5
Question 2.
Write a program to count the occurrences of each word in a given string?
Answer:
def word_count(str):
counts = dict ( )
words = str.split ( ) for word in words:
if word in counts:
counts[word] +=1
else:
counts[word]=1
return counts
print (word_count (‘the quick brown fox jumps over the lazy dog.’))
Ouput:
{‘the’: 2, ‘jumps’: 1, ‘brown’: 1, ‘lazy’: 1, ‘fox’: 1, ‘over’: 1, ‘quick’: 1, ‘dog’: 1}
Question 3.
Write a program to add a prefix text to all the lines in a string?
Answer:
import
text =
“‘Strings are immutable. Slice is a
substring of a main string. Stride
is a third argument in slicing operation'”
text_without_lndentation= textwrap.dedent (text)
wrapped = extwrap.fill (text_without_Indentation, width = 50)
print (textwrap.indent(wrapped, ‘*’)
print ()
Output:
- Strings are immutable. Slice is a
- substring of a main string. Stride
- is a third argument in slicing operation
Question 4.
Write a program to print integers with ‘*’ on the right of specified width?
Answer:
x = 1 2 3
print (“original number: “, x)
print (“formatted number(right padding, width 6): “+” {: * < 7 d}”.format(x));
Output:
original number : 1 2 3
formatted number (right padding, width 6): 1 2 3 ***
Question 5.
Write a program to create a mirror of the given string. For example, “wel” = “lew“?
Answer:
str1 = input (“Enter a string: “)
str2 = ‘ ‘
index= -1
for i in str1:
str2 += str1 [index]
index -= 1
print (“The given string = { } \n The Reversed string = { }”.format(str 1, str 2))
Output:
Enter a string: welcome
The given string = welcome
The Reversed string = emoclew
Question 6.
Write a program to removes all the occurrences of a given character in a string?
Answer:
def removechar(s,c):
# find total no of occurrence of a character
counts = s.count(c)
# convert into list of characters
s = list(s)
# keep looping until counts become 0
while counts:
# remove char, from list
s.remove(c)
counts -= 1
# join remaining characters s = ” .join(s)
print(s)
s = “python programming”‘
remove char(s, ‘p’)
Output:
ython rogramming
Question 7.
Write a program to append a string to another string without using + = operator?
Answer:
s1 = input (“Enter the first string: “)
s2 = input (“Enter the second string: “)
print (‘concatenated strings =’,” ” ,join ([s1, s2]))
Output:
Enter the first string: Tamil
Enter the second string: Nadu
concatenated strings = Tamil Nadu
Question 8.
Write a program to swap two strings?
Answer:
print (“Enter Y for exit.”)
string1 = input(“Enter first string: “)
if string1 = = ‘x’:
exit();
else:
string2 = input (“Enter second string : “)
print (” \n Both strings before swap : “)
print (“First string = “, string1)
print (” Second string = “, string2)
temp = string1
string1 = string2
string2 = temp
print (“\n Both strings after swap: “)
print (“First string = “, string1)
print (” Second string = “, string2)
Output:
Enter ‘x’ for exit
Enter first string: code
Enter second string: python
Both strings before swap:
First string = code
Second string = python
Both strings after swap:
First string = python
Second string = code
Question 9.
Write a program to replace a string with another string without using replace ( )?
Answer:
s1 = input (“Enter the string to be replaced: “)
s2 = input (“Enter the string to replace with “)
s1 = s2
print (“Replaced string is “, s1)
Output:
Enter the string to be replaced: Computer
Enter the string to replace with: repcomputer
Replaced string is repcomputer
Question 10.
Write a program to count the number of characters, words and lines in a given string?
Answer:
string = input (“Enter string:”)
char = 0
word = 0
line = 0
for i in string:
char = char + 1
if (i = = “):
word = word + 1
elif (i = = ‘ \n’):
line = line +1
print (“Number of words in the string: “)
print (word)
print (“Number of characters in the string: “)
print (char)
print (“Number of lines in the string: “)
print (line)
Output:
Enter string: welcome to learning python
Number of words in the string : 4
Number of characters in the string : 26
Number of lines in the string : 1
Samacheer Kalvi 12th Computer Science Strings and String Manipulations Additional Questions and Answers
PART – 1
I. Choose The Correct Answer
Question 1.
Which of the following is used to handle an array of characters in python?
a) Functions
b) Composition
c) String
d) Arguments
Answer:
c) String
Question 2.
Strings which contains double quotes should be defined with …………………….. quotes
(a) Single
(b) Double
(c) Triple
(d) All the these
Answer:
(c) Triple
Question 3.
Which of the following allows the creation of multiline strings
a) ‘ ‘
b) ” ”
c) “”” “””
d) None of these
Answer:
c) “”” “””
Question 4.
In strings, the negative index assigned from the last character to the first character in reverse order begins with …………………………
(a) 0
(b) 1
(c) -1
(d) -2
Answer:
(c) -1
Question 5.
How will you modify the string?
(a) A new string value can be assigned to the existing string variable
(b) Updating the string character by character
Answer:
(a) A new string value can be assigned to the existing string variable
Question 6.
The positive subscript always starts with
a) 0
b) 1
c) -1
d) 0.1
Answer:
a) 0
Question 7.
Which command is used to remove the entire string variable?
(a) Remove
(b) Del
(c) Delete
(d) Strike
Answer:
(b) Del
Question 8.
The joining of two or more strings is called…………………………..
(a) Append
(b) Repeating
(c) Concatenation
(d) Strike
Answer:
(c) Concatenation
Question 9.
…………………. are immutable in python.
a) Characters
b) Strings
c) Numbers
d) Functions
Answer:
b) Strings
Question 10.
Find the wrongly matched pair from the following.
(a) Append ⇒ + =
(b) Concate ⇒ +
(c) Repeat ⇒ /
(d) Slice ⇒ [ ]
Answer:
(c) Repeat ⇒ /
Question 11.
Which operator is used to append a new string with an existing string?
(a) +
(b) + =
(c) *
(d) * =
Answer:
(b) + =
Question 12.
The multiplication operator is also called………………………….
(a) Append
(b) Concatenate
(c) Repeat
(d) Slice
Answer:
(c) Repeat
Question 13.
Which is used to display a string multiple times?
(a) Repeating
(b) *
(c) Multiplication operator
(d) All the above
Answer:
(d) All the above
Question 14.
…………………………. is a substring of the main string.
Answer:
Slice
Question 15.
The formatting operator is used to replacing parts of strings with the data stored in variables.
a) #
b) %
c) ::
d) ,
Answer:
b) %
Question 16.
Find the wrong statement from the following.
(I) Slice a single character from a string
(II) Slice a substring
(III) Slice a substring without specifying the beginning index
(IV) Slice a substring without specifying the end index
(a) (I), (II)
(b) (II), (III), (IV)
(c) All are wrong
(d) All are correct
Answer:
(d) All are correct
Question 17.
The default value of stride is …………………………
(a) 0
(b) 1
(c) n
(d) n – 1
Answer:
(b) 1
Question 18.
If the stride is negative, then it will prints
(a) Third character
(b) the Third word
(c) Full string
(d) Reverse order
Answer:
(d) Reverse order
Question 19.
…………………………. is the formatting character for signed decimal integer.
(a) %d or %i
(b) %d and %i
(c) %d %u
(d) %i &u
Answer:
(a) %d or %i
Question 20.
…………………….. is the formatting character for short numbers in floating-point or exponential notation.
Answer:
% g or % G
Question 21.
Escape sequences start with a ………………………..
Answer:
Back Slash
Question 22.
Find the wrong match
(a) Backslash – \b
(b) Backslash – //
(c) Carriage return – \r
(d) Line feed – \n
Answer:
(b) Backslash – //
Question 23.
Which function returns the length of the string?
(a) str len( )
(b) len(str)
(c) length( )
(d) strlength( )
Answer:
(b) len(str)
Question 24.
The function isalnum( ) returns ………………………. when it contains special characters.
Answer:
False
Question 25.
How many membership operators are there?
(a) 2
(b) 3
(c) 4
(d) 5
Answer:
(a) 2
Question 26.
……………………. is the membership operator.
(a) is
(b) at
(c) to
(d) in
Answer:
(d) in
Question 27.
……………………. function is a powerful function used for formatting strings.
Answer:
Format ( )
Question 28.
The ……………………. and ………………………. operators can be used with strings to determine whether a string is present another string.
Answer:
In, Not in
PART – II
II. Answer The Following Questions
Question 1.
Fill the Table with appropriate values.
Answer:
Question 2.
Find the output?
Answer:
Program
str1 = ‘ * ‘
i=1
while i<=5: print (str1*i)
i+=1
Output
*
* *
* * *
* * * *
* * * * *
PART – III
III. Answer The Following Question
Question 1.
Write a note on replacing function?
Answer:
The replace function replaces all occurrences of char 1 with char 2.
Example
>>> str1 =”How are you”
>>> print (str1)
How are you
>>>print (str1.replace(“o”, “e”))
Hew are yeu
Question 2.
Write a note on the function center ().
Answer:
Function :center ()
Syntax:
center (width, fillchar)
Description:
Returns a string with the original String centered to a total of width columns and filled with fillchar in columns that do not have characters.
Example:
>>>strl=”Welcome”
>>>print(strl center(15,’*’))
**** Welcome****
Question 3.
Give any 6 formatting characters with their usage?
Formatting characters
Answer:
Question 4.
Write any 6 escape sequences with their description?
Answer:
PART – IV
IV. Answer The Following Questions
Question 1.
Explain any 10 Built-in string functions?
Answer:
Built-in String functions
Python supports the following built-in functions to manipulate strings.