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

(iii) To get a string by removing all leading and trailing whitespace characters from a string.
              (iv) To check whether a string occurs as a substring of a given string.
              (v) To check whether the characters of a string are in lowercase.

         Ans:  (i)  isspace()
              (ii) upper()
              (iii) strip()
              (iv) find()
              (v) islower()
          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 Python Program that outputs 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:  txt = input('Enter a string: ')


              length = len(txt)
              finalTxt = ''
              for i in range(length):
                  if i%2 == 0:
                      finalTxt += txt[i].upper()
                  else:
                      finalTxt += txt[i]

              print('Original string: ', txt)
              print('String after replacing alphabetic characters at even indices by corresponding
              uppercase letters: ', finalTxt)
          13.  What will be the result of running the following Python code?
              name = 'ToP#1'
              for x in name:
                  if x.isalpha():
                      print("alphabet")
                  elif x.isdigit():
                      print("digit")
         278   Touchpad Computer Science-XI
   275   276   277   278   279   280   281   282   283   284   285