Strings and String Manipulation: book back answers
20 solved questions from the end of this chapter, Class 12 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.
← Notes and quiz for Strings and String ManipulationChoose the best answer (1 Mark)
Which of the following is the output of the following python code? str1="TamilNadu" print(str1[::-1])
- (a) Tamilnadu
- (b) Tmlau
- (c) udanlimaT
- (d) udaNlimaT
Try it, then show the answerAnswer
(c) udanlimaT
A negative stride prints the string in reverse order.
From the book: “If you specify a negative value, it prints in reverse order.”
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
Try it, then show the answerAnswer
(c) Type error
Strings are immutable, so assigning a new value to one character is not allowed.
From the book: “strings in python are immutable. That means, once you define a string modifications or deletion is not allowed.”
Which of the following operator is used for concatenation?
- (a) +
- (b) &
- (c) *
- (d) =
Try it, then show the answerAnswer
(a) +
The plus operator joins two or more strings.
From the book: “The plus (+) operator is used to concatenate strings in python.”
Defining strings within triple quotes allows creating:
- (a) Single line Strings
- (b) Multiline Strings
- (c) Double line Strings
- (d) Multiple Strings
Try it, then show the answerAnswer
(b) Multiline Strings
Triple quotes allow a string to span multiple lines.
From the book: “Defining strings within triple quotes also allows creation of multiline strings.”
Strings in python:
- (a) Changeable
- (b) Mutable
- (c) Immutable
- (d) flexible
Try it, then show the answerAnswer
(c) Immutable
Once defined, a string cannot be changed during execution.
From the book: “In python, strings are immutable, it means, once you define a string, it cannot be changed during execution.”
Which of the following is the slicing operator?
- (a) { }
- (b) [ ]
- (c) < >
- (d) ( )
Try it, then show the answerAnswer
(b) [ ]
The square brackets with index values are used to slice substrings.
From the book: “A substring can be taken from the original string by using [ ] operator and index or subscript values.”
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
Try it, then show the answerAnswer
(d) third argument of slice operation
Stride is the third slicing argument, specifying how many characters to move forward.
From the book: “you can specify a third argument as the stride”
Which of the following formatting character is used to print exponential notation in upper case?
- (a) %f
- (b) %E
- (c) %g
- (d) %n
Try it, then show the answerAnswer
(b) %E
The chapter lists %E as exponential notation, with uppercase E.
From the book: “%e or %E Exponential notation”
Which of the following is used as placeholders or replacement fields which get replaced along with format( ) function?
- (a) { }
- (b) < >
- (c) ++
- (d) ^^
Try it, then show the answerAnswer
(a) { }
Curly braces mark the replacement fields used by format().
From the book: “The curly braces { } are used as placeholders or replacement fields”
The subscript of a string may be:
- (a) Positive
- (b) Negative
- (c) Both (a) and (b)
- (d) Either (a) or (b)
Try it, then show the answerAnswer
(c) Both (a) and (b)
String subscripts can be positive or negative integers.
From the book: “The subscript can be positive or negative integer numbers.”
Part -II — Answer the following questions (2 Marks)
What is String?
Try it, then show the answerAnswer
A string is a sequence of Unicode characters, which may include letters, numbers, or special symbols, enclosed within single, double, or triple quotes.
Strings are a Python data type used to handle an array of characters.
From the book: “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.”
Do you modify a string in Python?
Try it, then show the answerAnswer
No. Strings are immutable, so individual characters cannot be modified. You can assign a new string to the variable, or use replace() to produce a temporary changed string without changing the original.
Python does not allow updating a character in a string. The replace() result does not affect the original string.
From the book: “The changes done through replace () does not affect the original string.”
How will you delete a string in Python?
Try it, then show the answerAnswer
Use the del command to remove the entire string variable, for example: del str1.
Python does not allow deleting just one character from a string, but del can remove the whole variable.
From the book: “Whereas you can remove entire string variable using del command.”
What will be the output of the following python code? str1 = “School” print(str1*3)
Try it, then show the answerAnswer
SchoolSchoolSchool
The * operator repeats a string the specified number of times.
From the book: “The multiplication operator (*) is used to display a string in multiple number of times.”
What is slicing?
Try it, then show the answerAnswer
Slicing is taking one or more substrings from a main string using the [ ] operator and index or subscript values.
The slice is a substring selected from the original string.
From the book: “Slice is a substring of a main string.”
Part -III — Answer the following questions (3 Marks)
Write a short about the followings with suitable example: (a) capitalize( ) (b) swapcase( )
Try it, then show the answerAnswer
(a) capitalize() returns a string with its first character capitalized. Example: "chennai".capitalize() gives "Chennai". (b) swapcase() changes the case of every character to its opposite. Example: "tAmiL NaDu".swapcase() gives "TaMIl nAdU".
capitalize() changes the first character to uppercase; swapcase() reverses the case of each character.
From the book: “swapcase( ) It will change case of every character to its opposite case vice-versa.”
What will be the output of the given python program? str1 = "welcome" str2 = "to school" str3=str1[:2]+str2[len(str2)-2:] print(str3)
Try it, then show the answerAnswer
weld
str1[:2] gives "we". The last two characters of "to school" are "ol", so concatenating them gives "weol". Actually, the expression selects str2[len(str2)-2:], which is "ol"; the output is "weol".
From the book: “Joining of two or more strings is called as Concatenation.”
What is the use of format( )? Give an example.
Try it, then show the answerAnswer
format() is used to format strings by replacing curly-brace placeholders with supplied values. Example: "The sum of {} and {} is {}".format(34, 54, 34+54) gives "The sum of 34 and 54 is 88".
The function inserts the given values into the replacement fields in the string.
From the book: “The format( ) function used with strings is very versatile and powerful function used for formatting strings.”
Write a note about count( ) function in python.
Try it, then show the answerAnswer
count() returns the number of occurrences of a substring within a string. The beginning and ending range arguments are optional; if omitted, the whole string is searched. The search is case-sensitive.
For example, "Raja Raja Chozhan".count('Raja') returns 2.
From the book: “Returns the number of substrings occurs within the given range.”
Part -IV — Answer the following questions (5 Marks)
Explain about string operators in python with suitable example.
Try it, then show the answerAnswer
Python provides operators to perform different operations on strings. These operators are useful to manipulate strings.
- Concatenation (+): The plus (+) operator joins two or more strings.
- Append (+=): The += operator adds a string to the end of an existing string.
- Repeating (*): The multiplication operator displays a string multiple times.
- String slicing ([ ]): The slicing operator takes one or more substrings from a main string using index or subscript values.
- Stride in slicing: A third argument can specify the stride, or number of characters to move forward after retrieving the first character.
- String formatting (%): The % operator constructs strings by replacing parts of them with data stored in variables.
Example: Concatenation: "welcome" + "Python" gives 'welcomePython'.
Thus, Python string operators support joining, appending, repeating, slicing and formatting strings.
From the book: “Python provides the following operators for string operations. These operators are useful to manipulate string.”