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

False
                  >>> ''.isspace()
                      False
                 Note that the function call ''.isspace() yields False because the empty string does not contain even a single
                 whitespace.

                 ●  s.startswith(subStr): The method startswith(subStr) tests whether the string s starts with the
                   string subStr, passed as a parameter. For instance, in the following example, the first and second function calls
                   returned True and False, respectively, as 'unethical' starts with 'un' and not with 'ethical'.

                  >>> 'unethical'.startswith('un')
                      True
                  >>> 'unethical'.startswith('ethical')
                      False
                  >>> 'hello'.startswith('')
                      True
                 Note that the function call 'hello'.startswith('') yields True, because the null string is a prefix of every
                 string and the expression '' + 'hello' yields  'hello'.
                 ●  s.endswith(subStr): The method endswith(subStr) tests whether the string s ends with the suffix
                   subStr, passed as an argument. For example,
                  >>> 'ethically'.endswith('ally')
                      True
                  >>> 'Bibasha Nayak'.endswith('Bibasha  Nayak')
                      False
                 Note that in  the function  call  'Bibasha Nayak'.endswith('Bibasha  Nayak') False,  as the string
                 'Bibasha Nayak' contains an extra blank.
                 ●  s.find(subStr): The method find(subStr) searches for the first occurrence of the given string subStr in
                   the string s and  returns the  index of the first occurrence of the string passed as an argument. If the string subStr
                   does not appear anywhere in the string s, the method find() returns -1 indicating the condition substring not
                   found. For example,
                  >>> colors = 'green, red, blue, red, red, green'
                  >>>  colors.find('red')
                      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')


                                                                                                            Strings   345
   354   355   356   357   358   359   360   361   362   363   364