Page 298 - CTS - CSA TP - Volume 2
P. 298

COMPUTER SOFTWARE APPLICATION - CITS




           Output:














           TASK 12: Write a program that uses slicing to extract a substring from a given string

           Code:
           # 12. slice
           text = input(“Enter the String: “)
           print(“Given String is “, text)

           sliced_text = text[6:11]
           print(f”Sliced text: {sliced_text}”)
           Explanation:
           1  User Input: The program prompts the user to enter a string using input. The entered string is stored in the
              variable text.
           2  Original Text Display: The program prints the original string using print(“Given String is “, text).
           3  Slicing: The program uses slicing (text[6:11]) to extract a portion of the string. In Python, slicing is used to
              create a new sequence (in this case, a substring) by specifying a range of indices.
           4  Result Display: The sliced text is stored in the variable sliced_text and is then printed using the print statement.
              The formatted string (f”...”) allows for a clear display of the original and sliced text.

              This example demonstrates how to use slicing to extract a substring from a user-entered string. The specific
              range [6:11] extracts characters from the 7th to the 10th positions (Python uses 0-based indexing). Users can
              input different strings, and the program will display the sliced portion.

           Output:















           TASK 13: Write a program that converts a list into a tuple using the tuple function
           Code :
           # 13. tuple
           example_list = [1, 2, 3, 4, 5]
           print(“ Given List is : “, example_list)

           tuple_from_list = tuple(example_list)
           print(f”Tuple from list: {tuple_from_list}”)




                                                           283
                              CITS : IT & ITES - Computer Software Application - Exercise 132
   293   294   295   296   297   298   299   300   301   302   303