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

>>> name
                    'Komal'
               >>> id(name)
                    2594103348336
              It is important to note that Python has created a new string object while executing the assignment statement:
                         name = name[0] + 'o' + name[2:]


              12.3 String Methods
              Python provides several functions (also called methods) for manipulating strings.  As a string is an immutable object,
              all the methods that operate on a string leave the original string unchanged. To invoke a method associated with
              a string object, the string object is followed by a dot, followed by the name of the method, followed by a pair of
              parentheses that enclose the arguments (if any) required for invoking the method. For example, given a string object,
              the method count returns the number of occurrences of the string, say, string, passed as the argument to the
              method count(). The syntax to invoke method count() is as follows:

                                                       s.count(<string>)
              Consider the following function calls, which yield counts of occurrences of substrings 'e', 'excel', and 'excels'
              in 'excellent'.

               >>> 'excellent'.count('e')
                    3
               >>> 'excellent'.count('excel')
                    1
               >>> 'excellent'.count('excels')
                    0
              Next, let us find the number of vowels in the string 'Encyclopedia'.
               >>> vowels = 'AEIOUaeiou'
               >>> vowelCount = 0
               >>> 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 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.title()
                    "Hello 'How Are You?' I Am Good, How Are You?"


               342   Touchpad Computer Science (Ver. 2.0)-XI
   351   352   353   354   355   356   357   358   359   360   361