TN Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 1.
What is known as Accessing characters in a string?
Answer:
Python allocate an index value for its each character. These index values are otherwise called as subscript which are used to access and manipulate the strings.

Question 2.
What is replace () and write its syntax?
Answer:
replace () function to change all occurrence of a particular character in a string.
The syntax is replace(“ char1”, “char2”)

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 3.
What is meant by string concatenation?
Answer:

  • Joining of two string is called as string concatenation.
  • The plus (+) operator is used to concatenate string in python.

Question 4.
What is known as string formatting operators?
Answer:

  • The string formatting operator is one of the most exciting feature of python.
  • The formatting operator % is used to construct strings, replacing parts of the strings with the data.

Question 5.
Short note on Escape sequences in python.
Answer:

  1. Escape sequences start with a backslash (\) and it can be interpreted differently.
  2. When you have use single quote to represent a string, all the single quotes inside the string must be escaped. Similar in the case with double quotes.
    Eg: >>> print (“They said, “what\s there?”)

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 6.
What are called membership operators?
Answer:
The ‘in’ and ‘not in’ operators can be used with string to determine whether a string is present in another string. Therefore, these operators called as membership operators.

Question 7.
What is the output?
Answer:
>>>strl = “slice.substrings”
>>>print(strl[::4])
The output is curs

Question 8.
Short note on chr(ASII).
Answer:
Chr(ASII) returns the character represented by a ASCII.
Eg: >>>print (ch(98))
Output b

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 9.
How can you allocate an index value of subscript?
Answer:

  1. The positive subscript 0 is assigned to the first character and n-1 to the last character, when n is the number of characters in the string.
  2. The negative index assigned from the last character to the first character in reverse order begins with -1.

Question 10. What is output?
Answer:
>>> “print (“School”.replace(“o”,”e”))
The output will be printed as school.

Question 11.
What is output?
Answer:
>>> strl = “school”
>>> print (strl[0:3))
The output is sch.

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 12.
What is output?
Answer:
>>> strl = “Ramakrishna mission”
>>> print (stri[:: -2]
Output:
nisM nhikmR

Question 13.
What is output?
Answer:
strl = “G”
i = 1
while i <=5 :
print (str!*i)
i+=1
output
G
G G
G G G
G G G G
G G G G G

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 14.
Write a python program using Format( ) function.
Answer:
num1=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 15.
Write a short note on modifying and deleting strings.
Answer:

  1. Strings in python are immutable.
  2. That means, once you define a string modifications or deletion is not allowed.
  3. If you want to modify the string, a new string value can be assign to the existing string variable.

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 16.
Write a python program to slice substring.
Answer:
strl = “WELCOME”
index = 0
for i in srtl:
print (strl [: index+1])
index+=1

Question 17.
Explain swap case () function with example.
Answer:
Swap case( ) function will change case of every character to its opposite case vice- versa.
Eg:
>>> strl+ “COmpUteR SCiENcE”
>>> print (strl. swapcase())
The output will be printed as coMPuTEr scIenCe.

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 18.
Write a program to create an abecedarian series.
Answer:
str1 = “A B C D E F G H”
str2 = “ate”
for i in str1:
print ((i+str2), end = ‘\t’)
Output: Aate Bate Cate Bate Fate Gate Hate

Question 19.
What is known as stride when slicing string?
Answer:
When the slicing operation, you can specify a third argument as the stride. Which refers to the number of characters to move forward after the first character is retrieved from the string.
The default value of stride is 1.
Eg:
>>.strl=“l am studying TWELTH standard”
>>>print(str1 [13:20:3])
Output E.

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 20.
Write any five formatting characters and its usage.
Answer:

Format characters  Usage
%c  Character
%d or %i  Signed decimal integer
%s  String
%u  Unsigned decimal integer
%o  Octal integer
%x or %X  Hexadecimal integer (lower case x refers a – f;  upper case X refers A – F)

Question 21.
Write any five escape sequences supported by python.
Answer:

Escape Sequence  Description
 \newline  backslash and newline ignored
 \\  Backslash
 \’  Single quote
 \”  Double quote
 \a  ASCII Bell
 \b  ASCII Backspace
 \f  ASCII Form feed
 \n  ASCII Linefeed

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 22.
Explain the following Built-in string functions is python.
Answer:
(a) len () (b) count ()

(a) len ( ) – This function returns the length of the given string.
The syntax is len (str)
Eg:
>>> A= “SCHOOL”
>>> print (len (A))
The output is 6.

(b) count ( ) – It returns the number of substrings occurs within the given range. Range (beg, end) arguments are optional, if it is not given, python searched in whole string.Search is case sensitive.
Eg:
>>> strl+ “RAM RAM RAM 1+1”
>>> print (srt 1. count(‘RAM’))
The output is 3
>>> print (str 1. count (‘A’))
output is 3
>>> print (str 1. count(‘S’))
output is 0.

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 23.
What is string?
Answer:
In python, Anything (Numbers, characters, special symbols) can be created with single or double or triple quotes is called string.
Eg“RAMA”, “12 std”

Question 24.
Do you modify a string in python?
Answer:
No, because string in python are immutable. So we cannot modify a string in python.

Question 25.
How will you delete a string in python?
Answer:

  1. Python will not allow deleting a particular character in a string.
  2. Whereas we can remove entire string variable using del command.

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 26.
What will be the output of the following python code?
Answer:
strl = “School”
print (strl * 3)
The output as,
school school school

Question 27.
What is slicing?
Answer:
Slice is a substring of a main string. A substring can be taken from the original string by using [ ] operator and index values.
Thus [ ] is also known as slicing operator.

Question 28.
Write a Python program to display the given pattern
COMPUTER
COMPUTE
COMPUT
CO M P U
COMP
COM
CO
C
Answer:
Strl = “COMPUTER”
index = 0
for i in str1:
print (str1[index+1:])
index+=1

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 29.
Write a short about the followings with suitable example:
(a) capitalize( )
(b) swapcase( )
Answer:
(a) capitalize () – It is used to capitalize the first character of the string.
Eg:
>>> city = “Chennai”
>>>print (city, capitalize ())
The output as Chennai.

(b) swapcase( ) – It will change case into opposite case of character, vice-versa.
Eg:
»>strl = “rKM SCHOOL”
>>print(strl.swapcase())
The output as Rkm school.

Question 30.
What will be the output of the given python program?
Answer:
str1 = “welcome”
str2 = “to school”
str3=strl[:2]+str2[len(str2)-2:]
print(str3)
The output will be printed as
we 7

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 31.
What is the use of format()? Give an example.
Answer:
The format ( ) function used with strings is very versatile and powerful function used for formatting strings.
The curly braces { } are used as placeholders or replacement fields, which get replaced along with format () function.
Eg:
(Let n1=50, n2=100)
n1= int (input (“Number”))
n2= int (input(“Number”))
print (“The sum of {} and {} is {}”.
Format (nl, n2 (nl+n2)))
The output will be printed as
The sum of 50 and 100 is 150.

Question 32.
Write a note about count() function in python.
Answer:
Count function returns the number of substrings occurs within the given range.
The syntax is count (str, beg, end)
Eg:
>>> strl = “COMPUTER SCIENCE”
>>> print (strl. count (‘COMPUTER’))
The output is 1
>>> print (strl. count (‘C’)) .
The output is 3
Python searching in whole string. Search in case sensitive.

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 33.
Explain about string operators in python with suitable example.
Answer:
String operators in python:
(i) (Concatenation (+) – joining of two of more strings is called as concatenation. The plus (+) operator is used to concatenate string in python.
Eg:
>>> “COMPUTER” + “SCIENCE”
The output is COMPUTERSCIENCE
(ii) 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.
Eg:
>>> strl “My name is”
>>> strl+= “Rajesh”
>>> print (strl)
The output is My name is Rajesh

(iii) Repeating (*) – The multiplication operator (*) is used to display a string in multiple number of times.
Eg:
>>> strl = “CHENNAI”
print (strl*3)
The output will printed as
CHENNAI CHENN AI CHENNAI

(iv) Stride slicing – Slice a substring of a main string. A substring can be taken from the original string by using [ ] operator and index or subscript values. .
Eg:
>>> strl = “EDUCATION”
>>> print (strl [:3]
>>> print (strl [7:]
The output will be printed as
EDU
ON

(v) Strinde when slicing string – when the slicing operation, you can specify a third argument as the stride.
Eg:
>>> srtl = “welcome to learn python”
>>> print (strl [10:16:4])
The output is
r

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 34.
Write a python program to find the length of a string.
Answer:
# program to find the length of a string
strl = input (“Type a string”)
print (len (strl))

Question 35.
Write a program to count the occurrences of each word in a given string.
Answer:
# program to count occurrence of each word in a given siring.
strings raw_ input (“Type string”)
word= raw input (“Type word”)
a= [ ]
count= 0
a= string, split (“ ”)
for i in range (0, len(a)):
if(word = =a(i)):
count= count- 1
print (“Counted word is”)
print (count)

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 36.
Write a program to add a prefix text to all the lines in a string.
Answer:
# program to add a prefix text to all the lines in a string.
import text wrap
sample text – (‘ ‘)
Python is a widely used high-level general purpose, interpreted, dynamic programing language.
text_without_indentation= textwrap. indent (sample text)
wrapped = textwrap. fill (text without indentation, with =50)
Finalresult- textwrap. indent (wrapped,‘>’)
print ()
print (final_result)
print ()

Question 37.
Write a program to print integers with on the right of specified width.
Answer:
# pgm print integers with ‘*’ on the right of specified width.
x=3
y=123
print (“In original number”,x)
print (“Formatted Number (right padding, width 2) “+”{:*<3d}. format (x));
print (“Original number”,y)
print (“Formatted number (right padding, width 6): “+”{:*<7d}”. Format (y));
print ()

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 38.
Write a program to create a mirror of the given string. For example, “wel” = “lew”.
Answer:
# program to create a mirror of a given string.
strl = input ( )
Valid = True
for char in strl:
If not mirrored [char] in strl:
break
else:
(or)
>>> mirror (‘wood’)
The output is ‘boow’

Question 39.
Write a program to removes all the occurrences of a give character in a string.
Answer:
# program to removes of given character
string strl- (“Type text to remove”)
str. replace (‘x’,|‘ ’); //replace with space

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 40.
Write a program to append a string to another string without using += operator.
Answer:
# program to append a string
str1=“SENTHIL”
srt2= “KUMAR”
newstr= “ ’’.join ((strl, str2))
(or)
var1= “SENTHIL”
var2= “KUMAR”
var3=f“ {varl} {var2}”
print (var3)
(or)
str1= “SENTHIL”
str2= “KUMAR”
s= srtl.___add___(str2)
print (s)

Question 41.
Write a program to swap two strings.
Answer:
# program to swap two springs.
strl= input (“Type First String to swap”)
str2= input (“Type Second String to Swap”)
print (“\n Both String Before Swap”)
print (“First string= ”, srtl)
print (“Second string= ”, srt2) t= srtl strl= srt2 str2= t
print (“\n Both string affer swap”)
print (“First string=”, srtl)
print (“Second string=’, str2)

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 42.
Write a program to replace a string with another string without using replace( ).
Answer:
# program to replace a string with another string without using replace ( )
str1 = “string is a data type in python, strings are immutable”
result = ( )
for i in str1:
if i – =‘0’:
result +=1
print result
(OR)
result = [ ]
for i in strl:
if i = = ‘0’:
i = ‘0’:
result.append (i)
print1’. join (result)

Question 43.
Write a program to count the number of characters, words and lines in a given string.
Answer:
# program to count the number of characters, words and lines
string = raw_input(“Type string” )
char= 0
word= 1
line= 0
for i in string:
char = char +1
if(i = ‘ ’):
word= word +1
if (i =’ ‘):
line= line +1
print (“Number of words in the string”)
print (word)
print (“Number of character in the string”)
print (char)
print (“Number of lines in the string”)
print (line)

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Choose the best answer:

Question 1.
Which is the first positive character assigned to the subscript?
(a) 0
(b) 1
(c) 2
(d) 3
Answer:
(a) 0

Question 2.
Which is the last positive character assigned to the subscript?
(a) n
(b) n<sup>2</sup>
(c) n – 1
(d) 1 – n
Answer:
(c) n – 1

Question 3.
Which operator is used to append new string with an existing string?
(a) ++
(b) +=
(c) /=
(d) *=
Answer:
(b) +=

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 4.
Which multiplication operator is used to display a string is multiple number of times?
(a) x
(b) **
(c) ^
(d) *
Answer:
(c) ^

Question 5.
What is the output?
>>> strl= “GOOD”
>>> print (strl[0])
(a) G
(b) GO
(c) GOO
(d) Null
Answer:
(a) G

Question 6.
Which is the string formatting operator?
(a) //
(b) \\
(c) %
(d) &
Answer:
(c) %

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 7.
Which is the Hexadecimal formatting character?
(a) %H
(b) %X
(c) %d
(d) %e
Answer:
(b) %X

Question 8.
Escape sequences starts with a:
(a) /
(b) \
(c) %
(d) //
Answer:
(b) \

Question 9.
Which escape sequences is used for carriage return?
(a) \c
(b) \f
(c) \r
(d) \t
Answer:
(b) \f

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 10.
Which is the output of >>> print(ord(‘B’))?
(a) 65
(b) 66
(c) 67
(d) 68
Answer:
(b) 66

Question 11.
What is the output of >>> print (chr (97)) ?
(a) a
(b) A
(c) b
(d) B
Answer:
(a) a

Question 12.
Which command is used to remove entire string variable?
(a) era
(b) del
(c) ctrl+D
(d) Alt+D
Answer:
(b) del

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 13.
Match the following:

(i) +  (A) Slicing operator
(ii) +=  (B) Repeating operator
(iii) *  (C) Append
(iv) [ ]  (D) Concatenation

(a) (i) – D, (ii) – B, (iii) – A, (iv) – C
(b) (i) – D, (ii) – C, (iii) – B, (iv) – A
(c) (i) – C, (ii) – B, (iii) – A, (iv) – D
(d) (i) – C, (ii) – D, (iii) – A, (iv) – B
Answer:
(b) (i) – D, (ii) – C, (iii) – B, (iv) – A

Question 14.
Match the following:

(i) %c  (A) decimal integer
(ii) % d  (B) character
(iii) %X  (C) Floating point integer
(iv) %f  (D) Hexadecimal integer

(a) (i) – B, (ii) – A, (iii) – D, (iv) – C
(b) (i) – B, (ii) – C, (iii) – D, (iv) – A
(c) (i) – C, (ii) – D, (iii) – A, (iv) – B
(d) (i) – C, (ii) – D, (iii) – B, (iv) – A
Answer:
(a) (i) – B, (ii) – A, (iii) – D, (iv) – C

Question 15.
Match the following:

(i) \a  (A) Ascii carriage return
(ii) \b  (B ) Ascii Bell
(iii) \n  (C ) Ascii Backspace
(iv) \r  (D ) Ascii Linefeed

(a) (i) – B, (ii) – D, (iii) – A, (iv) – C
(b) (i) – B, (ii) – C, (iii) – D, (iv) – A
(c) (i) – C, (ii) – D, (iii) – A, (iv) – B
(d) (i) – C, (ii) – A, (iii) – B, (iv) – D
Answer:
(b) (i) – B, (ii) – C, (iii) – D, (iv) – A

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 16.
Choose the incorrect pair:

Column I  Column II
(a) %  Formatting operator
(b) % u  unsigned decimal integer
(c) % i  unsigned integer
(d) % e  exponential notation

Answer:
(c)

Question 17.
Choose the incorrect statement:
(a) len (str) is returns the length of the string.
(b) Capitalize () function is used to capitalize the all the character of the string.
(c) isalpha () is returns only letters.
(d) isdigit () is returns only numbers.
Answer:
(b) Capitalize () function is used to capitalize the all the character of the string.

Question 18.
Pick the odd one out:
(a) lower ( )
(b) upper ( )
(c) title ( )
(d) format ( )
Answer:
(d) format ( )

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 19.
Assertion (A):
Usually python does not support any modification in its strings.
Reason (R):
But, it provides a function replace ( ) to change all occurrences of a particular character in a string.
(a) Both A and R are True, And R is the Correct explanation for A.
(b) Both A and R are True, But R is not the correct explanation for A.
(c) A is True, But R is false.
(d) A is False, But R is True.
Answer:
(a) Both A and R are True, And R is the Correct explanation for A.

Question 20.
Assertion (A):
The string formatting operator is one of the most exciting feature in python.
Reason (R):
The formatting operator + is used to construct strings with the data stored in variables.
(a) Both A and R are True, And R is the Connect explanation for A.
(b) Both A and R are True, But R is not the correct explanation for A.
(c) A is True, But R is false.
(d) A is False, But R is True.
Answer:
(c) A is True, But R is false.

Question 21.
Choose the incorrect pair:
(a) %o – octal integer
(b) %e – decimal integer
(c) %f – exponential
(d) %g – character
Answer:
(a) %o – octal integer

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 22.
Choose the correct pair:
(a) \t – horizontal tab
(b) \v – form feed
(c) \ooo – octal value
(d) \a – new line
Answer:
(a) \t – horizontal tab

Question 23.
Choose the incorrect statement:
(a) Slice is a substring of a main string.
(b) The slicing operator is &.
(c) The default value of stride is 0.
(d) We cannot use negative value as a stride.
Answer:
(a) Slice is a substring of a main string.

Question 24.
Choose the correct statement:
(a) String is a sequence of numbers.
(b) String is a data type in Python.
(c) String is enclosed with only single quote.
(d) Python string are mutable.
Answer:
(b) String is a data type in Python.

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 25.
The default value of stride is:
(a) 0
(b) 1
(c) 3
(d) 5
Answer:
(b) 1

Question 26.
Which is first negative value assigned to the subscript?
(a) 0
(b) 1
(c) -1
(d) n – 2
Answer:
(c) -1

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 27.
The default value of string is:
(a) 0
(b) -1
(c) 1
(d) Nothing
Answer:
(c) 1

Question 28.
Which of the following is the output of the following python code?
strl=“TamilNadu”
print(str 1 [::-!])
(a) Tamilnadu
(b) Tmlau
(c) udanlimaT
(d) udaNlimaT
Answer:
(d) udaNlimaT

Question 29.
What will be the output of the following code?
strl = “Chennai Schools” strl [7] =
(a) Chennai-Schools
(b) Chenna-School
(c) Type error
(d) Chennai
Answer:
(c) Type error

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 30.
Which of the following operator is used for concatenation?
(a) +
(b) &
(c) *
(d) =
Answer:
(a) +

Question 31.
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

Question 32.
Strings in python:
(a) Changeable
(b) Mutable
(c) Immutable
(d) flexible
Answer:
(c) Immutable

Samacheer Kalvi TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 33.
Which of the following is the slicing operator?
(a) { }
(b) [ ]
(c) < >
(d) ( )
Answer:
(b) [ ]

Question 34.
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

Question 35.
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 TN State Board 12th Computer Science Important Questions Chapter 8 Strings and String Manipulations

Question 36.
Which of the following is used as placeholders or replacement fields which get replaced along with format( ) function?
(a) { }
(b) <>
(c) ++
(d) ^^
Answer:
(a) { }

Question 37.
The subscript of a string may be:
(a) Positive
(b) Negative
(c) Both (a) and (b)
(d) Either (a) or (b)
Answer:
(c) Both (a) and (b)

TN Board 12th Computer Science Important Questions