Page 331 - Computer Science Class 11 With Functions
P. 331

B.  State whether the following statements are True or False:
               1.  An attempt to modify a string will always yield an error.                                 _______
               2.  A multi-line string is always included in triple quotes.                                  _______
               3.  When a string is enclosed between double quotes, a double quote mark may be included as part of the string.   _______
               4.  Given an integer, the function string() yields the corresponding string.                  _______
               5.  Any integer can be converted to a string using the function str().                        _______
               6.  join() is used to concatenate two strings.                                                _______

            C.  Fill in the blanks.
               1.  The first character of a string will always have index ____________.
               2.  The function ____________ returns the number of characters in a string.
               3.  If a string contains both alphabets and numbers, the function, isalpha() will return ________.
               4.   If a string is enclosed between the double quote marks, then to include the double quote mark (") in the string, we precede
                  it with the symbol _____________.
               5.  A character in a string can be accessed using its __________.
               6.  The character sequence _____________ denotes a single end-of-line character.
            D.  Answer the following questions:
               1.  What is meant by indexing in the context of strings in Python?  Illustrate the use of negative and non-negative indexing with
                  the help of an example.
               2.  What is the error in the following code?
                  iMessage = "Taking Off"
                  iMessage[-1] = 'n'
               3.  What will be the output on execution of the following statements:
                  (i)
                 >>> msg1 = 'Welcome'
                 >>> msg2 = 'User'
                 >>> msg1 + msg2
                 >>> msg1 * 3
                 >>> print(len(msg1))
                  (ii)
                 >>> numApples = '34'
                 >>> numOranges = '12'
                 >>> print('I have', numApples + numOranges, 'fruits')
                  (iii)
                 >>> string ='I love Python.'
                 >>> string.split()
                 >>> string.partition(' ')
              4. Apply appropriate functions to the string poem to achieve the following:
                  poem  =  'We  live  in  a  beautiful  world.  Full  of  beauty,  majesty,  and  wonder.  Like  a
                  beautiful sea of diamonds and pearls.'
                  (i)  Find the number of characters in the string poem.
                  (ii)  Split the string into substrings on the basis of the delimiter period.
                  (iii)  Split the string into substrings on the basis of delimiter - and.
                  (iv)  Find the number of occurrences of the word -beautiful.
                  (v)  Find the index of the first occurrence of the word-beautiful.
                  (vi)  Find the index of the first occurrence of the word -beautiful from the right end of the string.
               5.  Examine the following statement and rectify the error in the following piece of code:

                 >>> msg = 'Try to be a rainbow in someone else's cloud.'
                 SyntaxError: unterminated string literal (detected at line 1)

                                                                                                       Strings   329
   326   327   328   329   330   331   332   333   334   335   336