Tamilnadu State Board New Syllabus Samacheer Kalvi 12th Computer Science Guide Pdf Chapter 8 Strings and String Manipulations Text Book Back Questions and Answers, Notes.
Tamilnadu Samacheer Kalvi 12th Computer Science Solutions Chapter 8 Strings and String Manipulations
12th Computer Science Guide Strings and String Manipulations Text Book Questions and Answers
I. Choose the best answer (1 Marks)
Question 1.
Which of the following is the output of the following python code?
str1=”TamilNadu”
print(strl[::-1])
a) Tamilnadu
b) Tmlau
c) udanlimaT
d) udaNlimaT
Answer:
d) udaNlimaT
Question 2.
What will be the output of the following code?
str1 = “Chennai Schools”
str1[7] =
a) Chennai-Schools
b) Chenna-School
c) Type error
d) Chennai
Answer:
a) Chennai-Schools
Question 3.
Which of the following operator is used for concatenation?
a) +
b) &
c) *
d) =
Answer:
a) +
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
Question 5.
Strings in python:
a) Changeable
b) Mutable
c) Immutable
d) flexible
Answer:
c) Immutable
Question 6.
Which of the following is the slicing operator?
a) {}
b) []
c) <>
d) ()
Answer:
b) [ ]
Question 7.
What is stride?
a) index value of slide operation
b) first argument of slice operation
c) the second argument of slice operation
d) third argument of slice operation
Answer:
d) third argument of slice operation
Question 8.
Which of the following formatting character is used to print exponential notation in the upper case?
a) %e
b) %E
c) %g
d) %n
Answer:
a) %e
Question 9.
Which of the following is used as placeholders or replacement fields which get replaced along with the format() function?
a) {}
b) <>
c) ++
d) ^^
Answer:
a) {}
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)
II. Answer the following questions (2 Marks)
Question 1.
What is String?
Answer:
String is a data type in python, which is used to handle 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”
Question 2.
Do you modify a string in Python?
Answer:
- No, because strings in python are immutable.
- That means once you define a string, modifications or deletion is not allowed.
- If we want to modify the string, a new string value can be assigned to the existing string variable.
Question 3.
How will you delete a string in Python?
Answer:
Python will not allow deleting a particular character in a string. Whereas you can remove the entire string variable using the del command.
Example: Code lines to delete a string variable
>>> str1=”How about you”
>>> print (str1)
How about you
>>> del str1
>>> print (str1)
NameError: name ‘str1’ is not defined
Question 4.
What will be the output of the following python code?
str1= “School”
print(str1*3)
Output:
School School School
Question 5.
What is slicing?
Answer:
String slicing:
Slice is a substring of a main string. A substring can be taken from the original string by using [ ] operator and index or subscript values. Thus, [ ] is also known as slicing operator. Using slice operator, we can slice one or more substrings from a main string.
General format of slice operation:
str[start:end]
Where start is the beginning index and end is the last index value of a character in the string. Python takes the end value less than one from the actual index specified. For example, if you want to slice first 4 characters from a string, you have to specify it as 0 to 5. Because python considers only the end value as n – 1.
Example: slice a single character from a string
>>> str1=”THIRUKKURAL”
>>> print (str1[0])
T
III. Answer the following questions (3 Marks)
Question 1.
Write a Python program to display the given pattern
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
Coding:
str 1=” COMPUTER”
index=len(str1)
for i in str1:
print(strl[0:index])
index-=1
Output:
>>>
RESTART: C:/Users/COMPUTER/
AppData/Local/Programs/Python/ Python37-32/compl.py ‘
COMPUTER
COMPUTE
COMPUT
COMPU
COMP
COM
CO
C
Question 2.
Write a short note about the following with suitable example:
(a) capitalize()
(b) swapcase()
Answer:
(a) Function: capitalize()
Syntax: capitalize ()
Description: Used to capitalize the first character of the string
Example:
>>>city=”chennai”
>>>print(city. capitalize())
Chennai
Function: swapcase ()
Syntax: It will change case of every ‘ character to its opposite case vice-versa.
Description:
Example:
>>>strl=”tAmil.NaDu”
>>>print (str1. swapcase ())
TaMIlnAdU
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:
>>>
RESTART: C:/Users/COMPUTER/
AppData/Local/Programs/Python/ Python37-32/ compl.py
weol
>>>
Question 4.
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.
Example:
num1 = int (input (“Number 1: “))
num2 = int (input (“Number 2: “))
print (“The sum of { } and { } is { }”.format (num1, num2,(num1 + num2)))
OutPut:
Number 1 : 34
Number 2 : 54
The sum of 34 and 54 is 88.
Question 5.
Write a note about count( ) function in python.
Answer:
Function: count()
Syntax: count (str, beg, end)
Description:
- Returns the number of substrings occurs within the given range.
- Substring may be a single character.
- Range (beg and end) arguments are optional. If it is not given, python searched in whole string.
- Search is case sensitive.
Example:
>>>strl=” Raja RajaChozhan >>> print(strl.count(/Raja’))
2
>>>print(strl.count(‘r’))
0
>>>print(strl.count(‘R’))
2
>>>prin(strl.count(‘a’))
5
>>>print (strl.count(‘a’,0.5))
2
>>>prin(strl.count(/a’,11))
1
IV. Answer the following questions (5 Marks)
Question 1.
Explain string operators in python with suitable examples.
Answer:
String Operators:
Python provides the following operators for string operations. These operators are useful to manipulate strings.
(i) Concatenation (+):
The joining of two or more strings is called Concatenation. The plus (+) operator is used to concatenate strings in python.
Example:
>>> “welcome” + “Python”
‘welcomePython’
(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.
Example:
>>> str1 =”Welcome to ”
>>> str1+=”Leam Python”
>>> print (str1)
Welcome to Learn Python
(iii) Repeating (*):
The multiplication operator (*) is used to display a string in multiple times.
Example:
>>> str1 =”Welcome”
>>> print (str1*4)
Welcome Welcome Welcome Welcome
(iv) String slicing:
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, you have to slice one or more substrings from the main string.
The general format of slice operation:
str[start: end]
Where start is the beginning index and end is the last index value of a character in the string. Python takes the end value less than one from the actual index specified. For example, if you want to slice the first 4 characters from a string, you have to specify it as 0 to 5. Because python considers only the end value as n – 1.
Example:
(i) slice a single character from a string
>>> str1=”THIRUKKURAL ”
>>> print (str1 [0])
T.
(v) Stride when slicing string
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.
Example:
>>> str1= “Welcome to learn Python”
>>> print (str1 [10:16])
learn
Note: Remember that, python takes the last value as n – 1
You can also use negative value as stride (third argument). If you specify a negative value, it prints in reverse order.
Example:
>>> str1 = “Welcome to learn Python”
>>> print(str1 [::-2])
nhy re teoIW
12th Computer Science Guide Chapter 8 Strings and String Manipulations Important Questions and Answers
I. Choose the best answer (1 Mark)
Question 1.
Strings in Python can be created using ………………………….. quotes
(a) Single
(b) Double
(c) Triple
(d) All the above
Answer:
(d) All the above
Question 2.
Strings are enclosed with
a) ”
b) ” ”
c) ”’ ”’
d) all of these
Answer:
d) all of these
Question 3.
The positive subscript of the string starts from ………………………….. and ends with …………………………
Answer:
0, n – 1
Question 4.
Another name of String index values are
a) class
b) subscript
c) function
d) arguments
Answer:
b) subscript
Question 5.
…………… is used to access and manipulate the strings
a) Index value
b) Subscript
c) Parameters
d) a or b
Answer:
d) a or b
Question 6.
Which function is used to change all occurrences of a particular character in a string?
(a) Replace ( )
(b) Change ( )
(c) Edit ( )
(d) Append ( )
Answer:
(a) Replace ( )
Question 7.
The negative subscript is always begun with
a) 0
b) -1
c) 1
d) -1.0
Answer:
b) -1
Question 8.
Which of the following operators are useful to do string manipulation?
a) +, –
b) *,/
c) + *
d) ; ”
Answer:
c) + *
Question 9.
Adding more strings at the end of existing strings is ………………………….
(a) Append
(b) Concatenation
(c) Repeating
(d) Slicing
Answer:
(a) Append
Question 10.
Python provides a function to change all occurrences of a particular character in a string.
a) replace()
b) change ()
c) change all ()
d) repalce all ()
Answer:
a) replace()
Question 11.
The operator is used to append a new string with an existing string.
a) +
b) + =
c) * =
d) + +
Answer:
b) + =
Question 12.
The operator is used to display a string multiple times.
a) *
b) * *
c) * =
d) + +
Answer:
a) *
Question 13.
Escape sequences starts with a
a) /
b) \
c) //
d) \”
Answer:
b) \
Question 14.
In python, the end value is considered as ……………………….
(a) 0
(b) n
(c) n – 1
(d) 1
Answer:
(c) n – 1
Question 15.
The ……………. function is a powerful function used for formatting strings.
a) format ()
b) string ()
c) Slice ()
d) format string ()
Answer:
a) format ()
Question 16.
Formatting operator which is used to represent signed decimal integer.
a) %d or %i
b) %s or %c
c) %g or %x
d) % s or %e
Answer:
a) %d or %i
II. Answer the following questions (2 and 3 Marks)
Question 1.
Write the general format of slice operation.
Answer:
General format of slice operation: str[start:end]
Start is the beginning index and End is the last index value of a character in the string.
Python takes the end value less than one from the actual index specified.
Question 2.
What is meant by stride?
Answer:
- A slice is a substring of the main string.
- Stride is a third argument in the slicing operation 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.
Example:
>>> strl = “Welcome to learn Python”
>>>print(str1[::-2])
Output:
nhy re teolW
Question 3.
Write a note on Append Operator?
Answer:
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+=”Leam Python”
>>> print (str1)
Welcome to Learn Python
Question 4.
What is the use of find () function? Explain with an example.
Answer:
Function: find ()
Syntax:
find(sub[,start[, end]])
Description:
- The function is used to search the first occurrence of the substring in the given string.
- It returns the index at which the substring starts.
- It returns -1 if the substring does not occur in the string.
Example:
>>> strl=’mammals’
>>> strl.find(‘ma’)
0
On omitting the start parameters, the function starts the search from the beginning.
>>> str1.find(/ma/2)
3
>>> str1.find(‘ma’,2,4)
-1
Displays -1 because the substring could not be found between the index 2 and 4-1
Question 5.
Write a note on lower () and is lower () functions.
Answer:
Function lower ()
Syntax:
lower ()
Description:
Returns the exact copy of the string with all the letters in lowercase
Example:
>>> strl=’SAVE EARTH’
>>>print(strl.lower())
save earth
Function :islower()
Syntax:
islower()
Description:
Returns True if the string is in lowercase
Example:
>>>str1=’welcome’
>>> print (strl.islower())
True
Question 6.
Differentiate upper () and isupper ().
Answer:
upper () | isupper () |
Function :upper() Syntax: ‘upper() Description: Returns the exact copy of the string with all letters in uppercase Example: >>>str1=’welcome’ >>>print(str.upper()) WELCOME | Function: isupper() Syntax: isupper() Description: Returns True if the string is in uppercase. Example: >>>strl=’welcome’ >>>pr int (strl. isupper()) False |
Question 7.
What will be the output of the given Python program?
Answer:
str=”COMPUTER SCIENCE”
(a) print(str*2)
(b) print{str[0: 7])
Output:
Str=” COMPUTER SCIENCE”
i) print(str*2) → COMPUTER SCIENCE
COMPUTER SCIENCE
ii) print(str[0 : 7])→ COMPUTE
Question 8.
Write notes on (a) isalnum (),
(b) isalpha () and (c) isdigit ()
Answer:
Function: isalnum ()
Syntax: isalnum ()
Description:
- Returns True if the string contains only letters and digits. It returns False.
- If the string contains any special character like *, etc.
Example1:
>>>str1=’Save Earth’
>>>str1. isalnum()
False
The function returns False as space is an alphanumeric character.
Example 2:
>>>strl=,savelEarth’.
>>>isalnum()
True
Function: isalpha()
Syntax: isalpha ()
Description:
Returns True if the string contains only letters Otherwise return False.
Example:
>>> str1=’SaveiEarth’
>>>str1.isalpha()
False
>>> str1-‘SaveEarth’
>>>str1 .isalpha ()
True
>>>
Function: isdigit ()
Syntax: isdigit ()
Description:
Returns True if the string contains only numbers. Otherwise it returns False
Example 1:
>>>str1=’SavelEarth’
>>>str1.isdigit()
False
Example 2:
>>> str1=’12345′
>>>str1.isdigit()
True
>>>
Question 9.
Write notes on Formatting Characters
Answer:
Format Characters | Usage |
%c | Character |
%d (or) %i | Signed decimal integer |
%s | String |
%u | Unsigned decimal integer |
%0 | Octal integer |
%x or %X | Hexadecimal integer (lower case x refers a-f; upper case X refers A-F) |
%e or %E | Exponential notation |
%f | Floating point numbers |
%g or %G | Short numbers in floating-point or exponential notation. |
Question 10.
Give the general format of replace function.
replace(“char1”, “char2″)
The replace function replaces all occurrences of charl with char2.
Example:
>>> str1=”How are you”
>>> print (strl)
How are you
>>>print (str1.replace(“o”, “e”))
Hew are yeu
Question 11.
Write a note on Escape Sequence in 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 |
\r | ASCII Carriage Return |
\t | ASCII Horizontal Tab |
\v | ASCII Vertical Tab |
\ooo | A character with an octal value 000 |
\xHH | A character with hexadecimal value HH |
Question 12.
What will be the output of the following Python code?
Strl = “Madurai”
print(Strl*3)
Output:
Madurai Madurai Madurai
Question 13.
What will be output of the following Python snippet?
strl=” THOLK APPIY AM” print(strl[4:])
print(strl[4::2])
print(strl[::3])
print(strl[::-3])
Output:
Question 14.
What will be the output of the following python program?
str1 = “welcome”
str2 = “to school”
str3 = strl[:3]+str2[len(str2)-1:] print(str3)
Output: Well
III. Answer the following questions (5 Marks)
Question 1.
Write a python program to check whether the given string is palindrome or not.
Answer:
str1 = input (“Enter a string:”)
str2 =”
index=-1
for i in strl:
str2 += str1 [index]
index -=1
print (“The given string = {} \n The Reversed string = {}”.format(strl, str2)) if (strl==str2):
print (“Hence, the given string is Palindrome”)
else:
print (“Hence, the given is not a palindrome”)
Question 2.
Write a python program to display the number of vowels and consonants in the given string.
Answer:
str1=input (“Enter a string:”)
str2=” a AeEiloOuU”
v,c=0,0
for i in str1:
if i in str2: .
v+=1
else:
c+=1
print (“The given string contains {} vowels and {} consonants.format(v.c))
Question 3.
Explain how the positive and negative subscript values are assigned? Give example.
Answer:
The positive subscript 0 is assigned to the first character and n-1 to the last character, where n is the number of characters in the string. The negative index assigned from the last character to the first character in reverse order begins with -1.
String | S | C | H | O | O | L |
Positive subscript | 0 | 1 | 2 | 3 | 4 | 5 |
Built-in functions | -6 | -5 | -4 | -3 | -2 | -1 |
Question 4.
Explain “Membership Operators suitable example.
Answer:
- The ‘in’ and ‘not in’ operators can be used with strings to determine whether a string is present in another string.
- Therefore, these operators are called Membership Operators.
Example:
strl=input (“Enter a string:”)
str2=” Chennai”
if str2 in str1:
print (“Found”)
else:
print (“Not Found”)
Output:1
Enter a string: Chennai GHSS, Saidapet Found
Output:2
Enter a string: Govt GHSS, Ashok Nagar Not Found
Question 5.
Write the output for the following Python commands: str1=”Welcome to Python”
(i) print(strl) (ii) print(strl[ll: 17])
(iii) print(strl[ll: 17 : 2])
(iv) print(strl[:: 4])
(v) print(strl[:: -4])
Output:
>>> strl=” Welcome to Python’
>>> print(strl)
Welcome to Python
>>> print(strl[ll: 17])
Python
>>> print(strl[ll: 17 : 2])
Pto
>>> print(strl[:: 4])
Wotyn
>>> print(strl[:: -4])
nytoW
Question 6.
Write a program to accept a string and print it in reverse order.
Answer:
Coding:
strl = input (“Enter a string:”)
Index=-1
while index > = -(lentstrll)):
print (“Subscript”,index,”] :”
+ strl [index])
index +=-1
Output:
Enter a string: welcome Subscript [ -1 ]: e
Subscript [ -2 ]: m
Subscript [ -3 ]: 0
Subscript [ -4 ]: c
Subscript [-5]: 1
Subscript [ -6 ] : e
Subscript [ -7 ] :w
Question 7.
Write a simple python program with list of five marks and print the sum of all the marks using while loop.
Answer:
Python Program:
mark-[]
for x in range(0,5): num=int(input(“Enter Mark:”))
mark+=(num,)
print(mark)
c=len(mark)
i=0
sum=0
while i<c:
sum+=mark[i]
i+=1
print(” Sum=” ,sum)
Output:
Enter Mark:60
Enter Mark:70
Enter Mark:80
Enter Mark:90
Enter Mark:100
[60, 70,80, 90,100]
Sum= 400