Page 269 - Computer Science Class 11 Without Functions
P. 269

7
             >>> colors.find('pink')
                 -1
            While scanning the string colors from left to right, the string 'red' appears at indices 7, 18, and 23. So, invoking
            colors.find('red') yields index 7, as the string 'red' appears first at index 7. However, since the substring
            pink is not present in the string, named colors, the function call colors.find('pink') yields -1.
            ●  s.rfind(subStr): The method  rfind()returns the index of the last occurrence of a substring in the given
              string. For example,
             >>> colors = 'green, red, blue, red, red, green'
             >>> colors.rfind('red')
                     23
            ●  s.index(subStr):  The method index(subStr) returns the index of the first occurrence of the substring
              passed as an argument. But if the string specified as the argument is not found in the given string, Python raises an
              exception. Thus, if the argument (subStr) is a substring of the given string (s), the methods find() and index()
              yield the same result. For example,

             >>> colors.find('blue')
                 12
             >>> colors.index('blue')
                 12
             >>> colors.find('pink')
                 -1
             >>> colors.index('pink')
                 Traceback (most recent call last):
                   File "<pyshell#42>", line 1, in <module>
                     colors.index('pink')
                 ValueError: substring not found
            ●  s.replace(old,  new): The method replace(old, new)  returns a string where all occurrences of the
              string old  are replaced by the string new.  For instance, in the following  function call, all the occurrences of
              'Amey' are replaced by 'Vihan' in the string, named message.
             >>> message = 'Amey my friend, Amey my guide'
             >>> message.replace('Amey', 'Vihan')
                 'Vihan my friend, Vihan my guide'

                  s.find(subStr): Returns the  index of the first occurrence of the string passed as an argument.
                  rfind(subStr): Returns the index of the last occurrence of a substring in the given string.
                  index(subStr): Returns the index of the first occurrence of the substring passed as an argument.
                  replace(old, new): Returns a string where all occurrences of the string old are replaced by the string new.


            11.5.1 Methods Involving Strings that Return a List

            ●  s.split(delimit): The method split(delimit) returns a list of substrings, separated by the given delimiter:
              delimit. The delimiter must be a string. When no delimiter is specified, Python splits the string on the default
              delimiter, whitespace, i,e, any of the characters: ' ', '\n', '\t' . For example, consider the following function
              calls where an explicit delimiter ',':
             >>> addressBook = 'Name:Rahul,Address:ND Block Pitampura,PhoneNumber:9953799950'
             >>> addressBook.split(',')
                 ['Name:Rahul', 'Address:ND Block Pitampura', 'PhoneNumber:9953799950']
             >>> friends = 'Aparajita, Animesh,, Aasrita, Pritam'
             >>> friends.split(',')
                 ['Aparajita', ' Animesh', '', ' Aasrita', ' Pritam']
            Note that when the string friends is split, spaces after the delimiter become part of the next string in the resultant
            list. Furthermore, note that the second and the third comma appear in the string friends adjacent to each other.


                                                                                                       Strings   267
   264   265   266   267   268   269   270   271   272   273   274