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

11.  Identify the errors (if any) in the given code:
                      message1 = "Good'
                      message2 = "Night"
                      num = 5
                      print(message1 + message2)
                      print(message1 * message2)
                      print(message1 + num)
                      print(message2 * num)
                  Ans.  message1 = "Goodꞌ   —> double quotes on both sides of string
                      message2 = "Night"
                      num=5
                      print(message1 + message2)
                      print(message1 * message2) —> two strings cannot be multiplied
                      print(message1 + num) —> A string cannot be concatenated with a number
                      print(message2 * num)
                   12.  Write a function in Python that returns a string, replacing every alphabetic character at even index (index 0, 2, 4, etc.) with
                      the corresponding uppercase letter. For example, if the string is "Welcome all," the output will be "WeLcOmE AlL".
                  Ans.  def alter(txt):
                          '''
                          Objective:  .
                          Input Parameter:a string
                           Return Value: a string that replaces alphabetic characters at even indices with
                           corresponding uppercase letters.
                          '''
                          length = len(txt)
                          finalTxt = ""
                          for i in range(0, length):
                              if i%2 == 0:
                                  finalTxt += txt[i].upper()
                              else:
                                  finalTxt += txt[i]

                              #if i<(length-1):
                               #   finalTxt += txt[i+1].upper()
                          return finalTxt

                      """ To use function alter()"""
                      myString = input("Enter a string:  ")
                      print("Original string: ", myString)
                      print("String after replacing alphabetic characters at even indices by corresponding
                      uppercase letters: ", alter(myString))
                   13.  What will be the result of running the following Python code?

                      def mixUp(name):
                            for x in name:
                                  if x.isalpha():
                                        print("alphabet")
                                  elif x.isdigit():
                                        print("digit")

                                                                                                            Strings   359
   368   369   370   371   372   373   374   375   376   377   378