{"id":28204,"date":"2022-08-28T14:59:59","date_gmt":"2022-08-28T09:29:59","guid":{"rendered":"https:\/\/python-programs.com\/?p=28204"},"modified":"2022-08-28T14:59:59","modified_gmt":"2022-08-28T09:29:59","slug":"python-slice-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-slice-function\/","title":{"rendered":"Python slice() function"},"content":{"rendered":"
In this article we are going to discuss about the slice() function in python with examples.<\/p>\n
slice() function in Python:<\/strong><\/p>\n Based on the specified range, the slice() method returns a portion of an iterable as an object of the slice class. It works with string, list, tuple, set, bytes, or range objects, as well as custom class objects that implement the sequence methods, __getitem__() and __len__().<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n NOTE:<\/strong><\/p>\n If only one parameter is specified, start<\/strong> and step<\/strong> are assumed to be None<\/strong>.<\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n Python supports “indexing from the end,” or negative indexing. Negative sequence indexes in Python represent positions from the array’s end. The slice()<\/strong> function can return negative values. In that case, the iteration will be performed backward, from end to start.<\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In this article we are going to discuss about the slice() function in python with examples. slice() function in Python: Based on the specified range, the slice() method returns a portion of an iterable as an object of the slice class. It works with string, list, tuple, set, bytes, or range objects, as well as …<\/p>\nslice(stop)\r\nslice(start, stop, step)<\/pre>\n
\n
Python slice() function<\/h2>\n
Method #1: Using slice() function on Strings<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = 'Hello this is PythonPrograms'\r\n# Slice the given string using the slice() function by passing start, stop \r\n# values as arguements to it(default stepsize is 1)\r\n# Here it returns the string in the index range 2 to 9(excluding the last index)\r\n# NOTE: It also considers the space as an index value\r\nsliceObj = slice(2, 10)\r\n\r\n# Print the result string after slicing\r\nprint(\"The result string after slicing = \", gvn_str[sliceObj])<\/pre>\n
The result string after slicing = llo this<\/pre>\n
It also considers the space as an index value<\/pre>\n
# Give the string as static input and store it in a variable.\r\ngvn_str = 'Hello this is PythonPrograms'\r\n\r\n# Pass only the stop value as an argument to the slice() function \r\n# to get the portion of the string in the given range\r\n# Here start and step values are considered 0, 1 respectively and \r\n# we get a string in the index range 0 to 9\r\nsliceObject_1 = slice(10)\r\n\r\n# Slice the given string by passing the start, stop, step values as arguments\r\n# slice() function and store it in a variable.\r\n# Here it returns the string from index 1 to 13(excluding last index) with the step value 2 \r\nsliceObject_2 = slice(1, 14, 2)\r\n \r\n# Print the result string 1 after slicing\r\nprint(\"The result string in the index range 0 to 9: \", gvn_str[sliceObject_1])\r\n# Print the result string 1 after slicing\r\nprint(\"The result string in the index range 1 to 13 with step=2: \", gvn_str[sliceObject_2])<\/pre>\n
The result string in the index range 0 to 9: Hello this\r\nThe result string in the index range 1 to 13 with step=2: el hsi<\/pre>\n
Method #2: Using slice() function on Lists<\/h3>\n
\n
\nwe get a list of elements in the index range of 0 to 4<\/li>\n# Give the list as static input and store it in a variable.\r\ngvn_list = [1, 3, 6, 8, 9, 4, 10, 14, 18]\r\n\r\n\r\n# Pass start, stop as arguments to the slice() function to get the part of the list\r\n# (default stepsize is 1) and store it in a variable.\r\n# Here it returns the list elements in the index range 2 to 6(excluding the last index)\r\nsliceObject_1 = slice(2, 7)\r\n\r\n# Pass only the stop value as an argument to the slice() function \r\n# to get the portion of the list in the given range\r\n# Here start and step values are considered 0, 1 respectively bu default and \r\n# we get a list elements in the index range 0 to 4\r\nsliceObject_2 = slice(5)\r\n\r\n# Slice the given list by passing the start, stop, step values as arguments\r\n# to the slice() function and store it in a variable.\r\n# Here it returnslist elements from index 1 to 7(excluding last index) with the step value 2 \r\nsliceObject_3 = slice(1, 8, 2)\r\n \r\n# Print the result list elements in the index range 2 to 6\r\nprint(\"The result list elements in the index range 2 to 6: \", gvn_list[sliceObject_1]) \r\n# Print the result list elements in the index range 0 to 4\r\nprint(\"The result list elements in the index range 0 to 4: \", gvn_list[sliceObject_2])\r\n# Print the result list elements in the index range 1 to 7 with step=2\r\nprint(\"The result list elements in the index range 1 to 7 with step=2: \", gvn_list[sliceObject_3])<\/pre>\n
The result list elements in the index range 2 to 6: [6, 8, 9, 4, 10]\r\nThe result list elements in the index range 0 to 4: [1, 3, 6, 8, 9]\r\nThe result list elements in the index range 1 to 7 with step=2: [3, 8, 4, 14]<\/pre>\n
Method #3: Using slice() function on Tuples<\/h3>\n
# Give the list as static input and store it in a variable.\r\ngvn_tuple = (1, 3, 6, 8, 9, 4, 10, 14, 18)\r\n\r\n\r\n# Pass start, stop as arguments to the slice() function to get the part of the \r\n# given tuple(default stepsize is 1) and store it in a variable.\r\n# Here it returns the tuple elements in the index range 2 to 6(excluding the last index)\r\nsliceObject_1 = slice(2, 7)\r\n\r\n# Pass only the stop value as an argument to the slice() function \r\n# to get the portion of the tuple in the given range\r\n# Here start and step values are considered 0, 1 respectively by default and \r\n# we get a tuple elements in the index range 0 to 4\r\nsliceObject_2 = slice(5)\r\n\r\n# Slice the given tuple by passing the start, stop, step values as arguments\r\n# to the slice() function and store it in a variable.\r\n# Here it returns the tuple elements from index 1 to 7(excluding last index)\r\n# with the step value 2 \r\nsliceObject_3 = slice(1, 8, 2)\r\n \r\n# Print the result tuple elements in the index range 2 to 6\r\nprint(\"The result tuple elements in the index range 2 to 6: \", gvn_tuple[sliceObject_1]) \r\n# Print the result tuple elements in the index range 0 to 4\r\nprint(\"The result tuple elements in the index range 0 to 4: \", gvn_tuple[sliceObject_2])\r\n# Print the result tuple elements in the index range 1 to 7 with step=2\r\nprint(\"The result tuple elements in the index range 1 to 7 with step=2: \", gvn_tuple[sliceObject_3])<\/pre>\n
The result tuple elements in the index range 2 to 6: (6, 8, 9, 4, 10)\r\nThe result tuple elements in the index range 0 to 4: (1, 3, 6, 8, 9)\r\nThe result tuple elements in the index range 1 to 7 with step=2: (3, 8, 4, 14)<\/pre>\n
Method #4: Using Negative Indexing<\/h3>\n
\nThis means that the last value in a sequence has a -1<\/strong> index, the second last has a -2<\/strong> index, etc.
\nIf you wish to select values from the end (right side) of an iterable, you can use negative indexing.<\/p>\n# Slicing the given list using negative indexing\r\n# Here -1 represents the last element of the list \r\ngvn_list = [1, 3, 6, 8, 9, 4, 10, 14, 18]\r\nsliceObject = slice(-1, -4, -1)\r\nprint(\"Slicing the given list = \", gvn_list[sliceObject])\r\n\r\n# Slicing the given string using negative indexing\r\ngvn_string = \"Pythonprograms\"\r\nsliceObject = slice(-1)\r\nprint(\"Slicing the given string = \", gvn_string[sliceObject])\r\n\r\n# Slicing the given tuple using negative indexing\r\ngvn_tuple = (1, 3, 6, 8, 9, 4, 10)\r\nsliceObject = slice(-1, -6, -2)\r\nprint(\"Slicing the given tuple = \", gvn_tuple[sliceObject])\r\n<\/pre>\n
Slicing the given list = [18, 14, 10]\r\nSlicing the given string = Pythonprogram\r\nSlicing the given tuple = (10, 9, 6)<\/pre>\n