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

●  s.join(list): Joins the strings in the given list (comma-separated sequence of values provided within square
           brackets) together using the string s as the delimiter. For example,
         >>> '>>'.join(['a', 'b', 'c', 'd'])
              'a>>b>>c>>d'
        The strings in the list ['a', 'b', 'c', 'd'] are joined together using '>>' as the delimiter.

        Next, let us consider an example that uses a space as the delimiter to print the concatenated string that spreads over
        multiple lines.

         >>>  ' '.join(['Hello', '\tHow', 'are', 'you?\n', 'I', 'am', 'fine.\n', 'How', 'are', 'you?'])
              'Hello \tHow are you?\n I am fine.\n How are you?'

         >>>  print(' '.join(['Hello', '\tHow', 'are', 'you?\n', 'I', 'am', 'fine.\n', 'How', 'are',
              'you?']))
              Hello      How are you?
              I am fine.
              How are you?



                 Write the names of the functions that will be used to perform the following tasks:
                   (i)  To convert a string to uppercase.
                   (ii)  To find the length of a string.
                  (iii)  To check whether a string consists of digit(s).
                  (iv)  To return a list of words from the string.
                   (v)  To display the index of a character in a string.
                  (vi)  To replace the character in a string with a new character.
                  (vii)  To return a string with the first character of each word capitalized.
                   (viii)  To remove the leading spaces from a string.





        12.4 Case Study
        In this section, we will develop a module named stringFns.py that enables us to perform the following operations
        on strings:

        1.   Form  a  string  comprising  distinct  characters  in  a  string,  replacing  uppercase  characters  with  corresponding
            lowercase characters.

        2.  Compute the number of matching characters in a pair of strings.
        3.  Find whether a string is a substring of another string.
        4.  Construct a string by reversing the sequence of the characters in the original string.

        12.4.1 Distinct Characters in a String Ignoring the Case

        To form a string comprising the distinct characters in a string, let us develop a function distinct() that takes a
        string str1 as the input parameter.

        ●  On execution of line 8, a string may comprise a mix of upper and lowercase characters, we first construct a string of
           lowercase characters.
        ●  Line 9, we initialize the string of distinct characters (distinctChars) to an empty string.
        ●  On execution of lines 10-12, for every character in the string, if the character is not included (not present) in the
           string distinctChars, it is concatenated to distinctChars.
        ●  On execution of line 13, finally, the function returns a string (distinctChars) of distinct characters.

         314   Touchpad Computer Science-XI
   311   312   313   314   315   316   317   318   319   320   321