Page 363 - Computer Science V2.0 Class 11
P. 363

str.                      Tests  whether  the  string  str  ends  >>>string_s1.endswith('ence')
                 endswith(subStr)          with  the  suffix  subStr,  passed  as  a  True
                                           parameter.
                 str.find(subStr)          Returns  the  index  of  the  first  >>>string_s1.find('e')
                                           occurrence of the string passed as an  15
                                           argument.
                 str.rfind(subStr)         Returns the index of the last occurrence  >>>string_s1.rfind('e')
                                           of a substring in the given string.  23

                 str.index(subStr)         Returns  the  index  of  the  first  >>>string_s1.index('en')
                                           occurrence of the substring passed as  21
                                           an argument.
                 str.replace(old,          Returns a string where all occurrences  >>> string_s1= 'Touchpad
                 new)                      of the string old are replaced by the  Computer Science'
                                           string new.                         >>> string_s1.replace('e','z')

                                                                               Touchpad Computzr Scizncz
                 str.split(delimit) Returns a list of substrings, separated  >>> string_s1= 'Touchpad
                                           by the given delimiter, delimit.    Computer Science'

                                                                               >>> string_s1.split(' ')
                                                                               ['Touchpad', 'Computer',
                                                                               'Science']
                 str.                      Splits the string into three parts: the  >>> string_s1= 'Touchpad
                 partition(delimit) part of the string before the delimiter,  Computer Science'
                                           the delimiter itself, and the part of the  >>> string_s1.partition(' ')
                                           string after the delimiter.
                                                                               ('Touchpad', ' ', 'Computer
                                                                               Science')
                 str.join(list)            Joins  the  strings  in  the  given  list  >>>'#'.join(['Touchpad',
                                           (comma-separated sequence of values  'Computer', 'Science'])
                                           provided  within  square  brackets)  Touchpad#Computer#Science
                                           together  using  the  string  str  as  the
                                           delimiter.

                 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.


                                                                                                            Strings   349
   358   359   360   361   362   363   364   365   366   367   368