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

>>> for ch in vowels:
         ...         vowelCount += 'Encyclopedia'.count(ch)
         >>> print(vowelCount)
                  5
        Now, we examine the functionality of some more methods on a string object, say, s:
        ●  s.lower(),  s.upper(): The method lower() returns the lowercase version of the string. Similarly, the
           method upper() returns the uppercase version of the string. For instance, as email ids are case-insensitive, two
           mail ids should be treated the same if they comprise exactly the same sequence of characters, irrespective of the
           upper or lower case. So, while comparing an email id entered by a user (emailId1) with the one stored in the
           system (emailId2), we convert both email-ids to the same case (lowercase or uppercase).
         >>> emailId1 = 'orange@gmail.com'
         >>> emailId2 = 'Orange@gmail.com'
         >>> emailId1 == emailId2
              False
         >>> emailId1.lower() == emailId2.lower()
              True
        As expected, being immutable, the strings emailId1 and emailId2 remain unchanged:

         >>> emailId1, emailId2
              ('orange@gmail.com', 'Orange@gmail.com')
        ●  s.title(): The method title() returns a string which has the first letter of every word of the original string
           converted to uppercase, while the remaining letters are converted to lowercase. For example,:

         >>> message = "Hello \'how are you?\' I AM GOOD, how are you?"
         >>> message.title()
              "Hello 'How Are You?' I Am Good, How Are You?"
        ●  s.capitalize(): The method capitalize() returns a string which has the first letter of first word in the
           original string converted to uppercase, while the remaining letters are converted to lowercase. For example,
         >>> message = "Hello \'How are You?\'I am Good, How are You?"
         >>> message.capitalize()
              "Hello 'how are you?'i am good, how are you?"

               s.lower(): Returns lowercase version of the string.
               s.upper(): Returns uppercase version of the string.
               s.title(): Returns a string which has the first letter of every word of original string converted to uppercase,
               while remaining letters are converted to lowercase.
               s.capitalize(): Returns a string which has the first letter of first word in the original string converted to
               uppercase, while the remaining letters are converted to lowercase.

        ●  s.strip(),  s.lstrip(),  and  s.rstrip():  The method strip() returns a string formed from the
           original string by removing the whitespace characters (space, tab, linefeed, return, form feed, and vertical tab)
           appearing at the beginning and end of the string. For example,
         >>> '       Hello How are you!      '.strip()
              'Hello How are you!'
        Note that the method removes only the leading and trailing spaces, the white space appearing elsewhere is retained.
        Let us examine another example,
         >>> msg = '''        Hello How are you!
         ... I am Good,    Sir,
         ... How are you        '''
         >>> msg1 = msg.strip()
         >>> msg1
              'Hello How are you!\Ni am Good,    Sir,\nHow are you'
         >>> print(msg1)
              Hello how are you!

         264   Touchpad Computer Science-XI
   261   262   263   264   265   266   267   268   269   270   271