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

C T  02     1.  Consider the string given below:
                             quote="Practice makes you Perfect"
                         2.  Write the output for the following Python statements:
                             (i)  quote[5:15]
                             (ii)  quote[-5:10]
                             (iii)  quote[-4:-15:3]
                             (iv)  quote[-4:-15:-1]
                             (v)  quote[4:10:2]
                             (vi)  quote[10:]
                             (vii) quote[:10]



            11.4 Constructing a New String by Replacing Parts of a String

            In one of the previous examples, we were not able to change the string Kamal to Komal as the strings are immutable.
            However, we may create a new string by replacing 'a' with 'o', i.e., by concatenating the following: character at
            index 0, letter 'o', and the slice of the string beginning at index 2 and up to the end.   Subsequently, the newly
            formed string can be assigned to the existing variable name as follows:
             >>> name = 'Kamal'
             >>> id(name)
                 2594101581488
             >>> name = name[0] + 'o' + name[2:]
             >>> 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:]

            11.5 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



                                                                                                       Strings   263
   260   261   262   263   264   265   266   267   268   269   270