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 Manipulation

Choose the best answer (1 Mark)

  1. 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 answer

    (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.”

  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
    Try it, then show the answer

    (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.”

  3. Which of the following operator is used for concatenation?

    • (a) +
    • (b) &
    • (c) *
    • (d) =
    Try it, then show the answer

    (a) +

    The plus operator joins two or more strings.

    From the book: “The plus (+) operator is used to concatenate strings in python.”

  4. 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 answer

    (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.”

  5. Strings in python:

    • (a) Changeable
    • (b) Mutable
    • (c) Immutable
    • (d) flexible
    Try it, then show the answer

    (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.”

  6. Which of the following is the slicing operator?

    • (a) { }
    • (b) [ ]
    • (c) < >
    • (d) ( )
    Try it, then show the answer

    (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.”

  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
    Try it, then show the answer

    (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”

  8. 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 answer

    (b) %E

    The chapter lists %E as exponential notation, with uppercase E.

    From the book: “%e or %E Exponential notation”

  9. 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 answer

    (a) { }

    Curly braces mark the replacement fields used by format().

    From the book: “The curly braces { } are used as placeholders or replacement fields”

  10. 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 answer

    (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)

  1. What is String?

    Try it, then show the answer

    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.”

  2. Do you modify a string in Python?

    Try it, then show the answer

    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.”

  3. How will you delete a string in Python?

    Try it, then show the answer

    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.”

  4. What will be the output of the following python code? str1 = “School” print(str1*3)

    Try it, then show the answer

    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.”

  5. What is slicing?

    Try it, then show the answer

    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)

  1. Write a short about the followings with suitable example: (a) capitalize( ) (b) swapcase( )

    Try it, then show the answer

    (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.”

  2. 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 answer

    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.”

  3. What is the use of format( )? Give an example.

    Try it, then show the answer

    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.”

  4. Write a note about count( ) function in python.

    Try it, then show the answer

    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)

  1. Explain about string operators in python with suitable example.

    Try it, then show the answer

    Python provides operators to perform different operations on strings. These operators are useful to manipulate strings.

    1. Concatenation (+): The plus (+) operator joins two or more strings.
    2. Append (+=): The += operator adds a string to the end of an existing string.
    3. Repeating (*): The multiplication operator displays a string multiple times.
    4. String slicing ([ ]): The slicing operator takes one or more substrings from a main string using index or subscript values.
    5. Stride in slicing: A third argument can specify the stride, or number of characters to move forward after retrieving the first character.
    6. 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.”

Next chapterLists, Tuples, Sets and Dictionary