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

Sr. No.                        Command                                       Output

                     28    code4.endswith('99')                                     True
                     29    code5.endswith('9')                                      True
                     30    code7.endswith(' ')                                      True
                     31    code7.rfind('s')                                         -1
                     32    code2.rfind('s')                                         20

                     33    code1.index('S')                                         0
                     34    code5.index('s')                                         Error
                     35    code2.replace('stay','Be')                               'Be alert and Be safe'
                     36    code5.replace('9','0')                                   'Call@000'
                     37    code6.replace('z','#')                                   'Call999'
                     38    code2.split()                                            ['stay', 'alert', 'and', 'stay', 'safe']
                     39    code2.split('y')                                         ['sta', ' alert and sta', ' safe']
                     40    code5.split('@')                                         ['Call', '999']

                     41    code1.partition(' ')                                     ('Stay', ' ', 'Safe')
                     42    code5.partition('@')                                     ('Call', '@', '999')
                     43    '*' .join(['stay','safe','and','secure'])                'stay*safe*and*secure'
                     44    '#'.join([code9,code3])                                  'safetyfirst#STAY AWARE'
                     45    code7.join([code5,code6,code4])                          'Call@999  Call999  999999999'

               Program 33: Write a program that accepts a string as input and displays the count of alphabets, digits, and special
               characters contained in it.

              Ans. def countCharacters(string):

                       '''
                        Objective : To calculate count of alphabets, digits and special characters
                        in a sring
                       Input Parameter : string
                       Return Value : alphabetCount, digitCount, specialCount – numeric values
                       '''
                       # Initialize counters for alphabets, digits, and special characters
                       alphabetCount = digitCount = specialCount = 0

                       # Iterate through each character in the input string
                       for char in string:
                           if char.isalpha():
                               alphabetCount += 1

                           elif char.isdigit():
                               digitCount += 1
                           else:
                               specialCount += 1
                       return alphabetCount, digitCount, specialCount




               510   Touchpad Computer Science (Ver. 2.0)-XI
   519   520   521   522   523   524   525   526   527   528   529