Slicing in Python: String Manipulation Techniques

Slicing in Python
Slicing in Python

Slicing in Python : Handwritten Notes

Here are some Handwritten notes and questions based on slicing in Python. These questions relate to practical scenarios that students may encounter in real applications, helping them understand the utility of Python slicing.

[doc id=24731]

Slicing in Python with Strings : Video Explanation

One-Word/Short Answer Questions:

  1. What is the syntax for slicing a list in Python?
    • list[start:end:step]
  2. What is the result of the expression list[2:5] on the list list = [10, 20, 30, 40, 50]?
    • [30, 40, 50]
  3. What does the slice list[:3] return on a list?
    • The first three elements of the list.
  4. What is the output of the following code? Pythonlst = [1, 2, 3, 4, 5] print(lst[::2]) Use code with caution.
    • [1, 3, 5]
  5. What will be the output of s = "Python"; print(s[-1:])?
    • n
  6. What does the slice list[::] do in Python?
    • It returns a copy of the entire list.
  7. What is the default step size when slicing a list?
    • 1
  8. In Python slicing, what does list[::3] do?
    • It returns every third element of the list.
  9. What is the result of the expression str[::-1] if str = "Hello"?
    • olleH
  10. What does lst[0:5:-1] do to the list lst = [10, 20, 30, 40, 50]?
  • It returns an empty list because the step size is negative and the start index is greater than the end index.

Output-Based Questions:

  1. What will be the output of the following code? Pythons = "CBSE" print(s[1:3]) Use code with caution.
    • SB
  2. Given lst = [1, 2, 3, 4, 5, 6, 7], what will be the output of print(lst[1:6:2])?
    • [2, 4, 6]
  3. What will be printed by the following code? Pythona = [10, 20, 30, 40, 50] print(a[::2]) Use code with caution.
    • [10, 30, 50]
  4. What is the result of this Python slicing operation? Pythonstr = "Computer" print(str[-3:-1]) Use code with caution.
    • ut
  5. What is the output of the following code snippet? Pythonlst = [11, 22, 33, 44, 55] print(lst[4:1:-1]) Use code with caution.
    • [55, 44, 33]

5-Mark Questions:

  1. Explain the slicing mechanism in Python with examples. How can you slice a string to extract specific characters?
    • Slicing Mechanism: Slicing is a powerful technique in Python that allows you to extract specific portions of a sequence (like a list or string) using a simple notation. The general syntax for slicing is sequence[start:end:step].
    • Extracting Specific Characters:
      • To extract characters from a string, you can use the same slicing syntax. For example, to extract the first three characters of a string s, you can use s[:3].
      • To extract every other character, you can use a step size of 2: s[::2]
      • To reverse a string, you can use a negative step size: s[::-1]
  2. Write a Python program that takes a string as input and prints the reverse of the string using slicing. Pythonstring = input("Enter a string: ") reversed_string = string[::-1] print("Reversed string:", reversed_string) Use code with caution.
  3. What is the difference between lst[start:end] and lst[start:end:step] in Python? Explain with an example.
    • lst[start:end] extracts a slice of the list from the index start (inclusive) to the index end (exclusive), with a default step size of 1.
    • lst[start:end:step] extracts a slice with a specified step size. The step size determines how many elements to skip between each extracted element.
  4. Explain how negative indices work in slicing. Demonstrate with a Python code example.
    • Negative indices are used to access elements from the end of the sequence. For example, -1 refers to the last element, -2 to the second-to-last, and so on.
    Pythonmy_list = [10, 20, 30, 40, 50] print(my_list[-1]) # Output: 50 print(my_list[-3:]) # Output: [30, 40, 50] Use code with caution.
  5. Write a Python program to demonstrate the use of slicing in lists, where you print a sublist from index 3 to index 7 (exclusive) and reverse the sublist using slicing. Pythonmy_list = [10, 20, 30, 40, 50, 60, 70] sublist = my_list[3:7] reversed_sublist = sublist[::-1] print("Sublist:", sublist) print("Reversed sublist:", reversed_sublist)

Real-World Example Questions:

  1. Extracting a Specific Range of Data from a List:
    • You are working with a list of temperatures for a week:
      temperatures = [30, 32, 31, 33, 34, 35, 36].
      Write a Python code to extract the temperatures from the 2nd to the 5th day (inclusive).
      Output the result.
  2. Slicing a String for a Username:
    • You have a string username = "john_doe_2024".
      Write a Python code to extract just the first part of the username (i.e., john) before the underscore (_).
  3. Extracting Even Index Elements from a List:
    • You have a list of student marks: marks = [45, 78, 23, 56, 89, 92, 74, 61].
      Write a Python code to extract all marks at even indices (0, 2, 4, 6) and print them.
  4. Reversing a Date String:
    • You are given a date string date = "2024-11-24".
      Write a Python program to reverse the string and print it as "42-11-2024".
  5. Slicing to Extract Year from Date:
    • A string date = "2024-11-24" contains a date in the format YYYY-MM-DD.
      Write a Python code to extract and print only the year (i.e., 2024) from the string using slicing.
  6. Selecting Specific Characters from a String:
    • You have the string text = "MachineLearning".
      Write a Python program to slice the string and print the characters starting from index 3 to index 10 (inclusive).
  7. Extracting Sublist from a List of Products:
    • You are managing an inventory system with the following list of product names:
      products = ['Apple', 'Banana', 'Carrot', 'Date', 'Eggplant', 'Fig', 'Grape'].
      Write a Python code to extract and print the products from index 2 to 5.
  8. Extracting Odd Indexed Elements from a List:
    • You have the list numbers = [12, 17, 23, 29, 34, 41, 56, 63].
      Write a Python program to extract and print the elements at odd indices (1, 3, 5, 7).
  9. Handling Date Formatting:
    • You are given a date string date = "2024-11-24".
      Write a Python code to format the date to show just the month and day (e.g., “11-24”).
  10. Slicing for Pagination in a List:
    • You have a list of 1000 items (e.g., pages of a book):
      items = ['page_1', 'page_2', ..., 'page_1000'].
      Write a Python program to extract the items for the 10th page to the 20th page using slicing.
  11. Reversing a List:
    • You are working with a list of customer orders:
      orders = [101, 102, 103, 104, 105].
      Write a Python code to reverse the list using slicing and print the reversed list.
  12. Extracting Every Third Element from a List:
    • You have a list of product prices: prices = [100, 200, 300, 400, 500, 600, 700, 800, 900].
      Write a Python code to extract every third price (i.e., prices at indices 0, 3, 6) and print them.
  13. Slicing for Middle Elements in a List:
    • You are analyzing a list of stock prices:
      stock_prices = [210, 250, 270, 300, 290, 280, 350].
      Write a Python code to extract and print the middle three elements from the list.
  14. Extracting a Range of Characters from a String:
    • You have a string sentence = "DataScienceIsFun".
      Write a Python code to extract the word “Science” using slicing.
  15. Selecting the Last Few Elements from a List:
    • You have a list of weekly sales: sales = [100, 200, 300, 400, 500, 600, 700].
      Write a Python program to extract the sales from the last 3 days of the week.
  16. Slicing to Get a Subset of a String:
    • You have a string address = "1234 Elm St, Springfield".
      Write a Python code to extract the street name (i.e., “Elm St”) using slicing.
  17. Accessing the Last Few Items of a List:
    • You are working with a list of the top 10 books:
      books = ['Book1', 'Book2', 'Book3', 'Book4', 'Book5', 'Book6', 'Book7', 'Book8', 'Book9', 'Book10'].
      Write a Python code to get the last 4 books from the list.
  18. Reversing a List and Removing Elements:
    • You have a list of student names: students = ['Alice', 'Bob', 'Charlie', 'David', 'Eve'].
      Write a Python code to reverse the list using slicing and remove the first element.
  19. Slicing with Negative Indices:
    • You have the string message = "Welcome to Python!".
      Write a Python program to extract the last 6 characters of the string using negative indices.
  20. Generating a Sublist for Weekly Reports:
    • You are working with a weekly report list:
      reports = ['Report1', 'Report2', 'Report3', 'Report4', 'Report5', 'Report6', 'Report7'].
      Write a Python code to generate and print the reports for the 3rd, 4th, and 5th days of the week using slicing.

Tags: String Slicing python , Strings in Python , Slicing in Python , CBSE 12th lectures , CBSE 11th String , String notes , CBSE 11th computer science

Review Your Cart
0
Add Coupon Code
Subtotal

 
Scroll to Top