Slicing (List, Tuple And String) In Python

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 79:-  Slicing (List, Tuple And String) In Python

 

Slicing is a powerful feature in Python that allows you to extract a portion (a subsequence) of a list, tuple, or string based on its index positions. Slicing uses the colon : as the separator, and the syntax for slicing is sequence[start:stop:step], where:

  • sequence: The list, tuple, or string from which you want to extract the subsequence.
  • start: The index of the first element to include in the subsequence (inclusive). If not specified, the default is the beginning of the sequence (index 0).
  • stop: The index of the first element to exclude from the subsequence (exclusive). The slice stops before this index. If not specified, the default is the end of the sequence.
  • step: The step size used for the slicing. It determines how many elements to skip between each included element. If not specified, the default step size is 1.

Let's see examples of slicing for lists, tuples, and strings:

  1. Slicing a List:

pythonCopy code

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Extract elements from index 2 to index 6 (exclusive) sub_list = my_list[2:6] print(sub_list)  # Output: [3, 4, 5, 6] # Extract every second element every_second = my_list[::2] print(every_second)  # Output: [1, 3, 5, 7, 9] # Reverse the list reversed_list = my_list[::-1] print(reversed_list)  # Output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

  1. Slicing a Tuple:

pythonCopy code

my_tuple = (10, 20, 30, 40, 50) # Extract elements from index 1 to index 4 (exclusive) sub_tuple = my_tuple[1:4] print(sub_tuple)  # Output: (20, 30, 40) # Extract every second element every_second = my_tuple[::2] print(every_second)  # Output: (10, 30, 50) # Reverse the tuple reversed_tuple = my_tuple[::-1] print(reversed_tuple)  # Output: (50, 40, 30, 20, 10)

  1. Slicing a String:

pythonCopy code

my_string = "Hello, World!" # Extract characters from index 2 to index 8 (exclusive) sub_string = my_string[2:8] print(sub_string)  # Output: "llo, W" # Extract every second character every_second = my_string[::2] print(every_second)  # Output: "Hlo ol!" # Reverse the string reversed_string = my_string[::-1] print(reversed_string)  # Output: "!dlroW ,olleH"

Slicing is a versatile feature in Python that allows you to manipulate sequences easily and efficiently. It's a fundamental technique used frequently in data processing and manipulation tasks.

9. List

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?