{"id":2794,"date":"2020-01-23T11:12:35","date_gmt":"2020-01-23T11:12:35","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2794"},"modified":"2022-08-06T13:10:32","modified_gmt":"2022-08-06T13:10:32","slug":"python-slice-function","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/built-in-methods\/python-slice-function","title":{"rendered":"Python slice() function"},"content":{"rendered":"\n<p><em>Python slice() function <\/em>returns a sliced object from the set of indexes of the input specified by the user in accordance with the arguments passed to it.<\/p>\n\n\n\n<p>Thus, it enables the user to slice any sequence such as <a href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" class=\"rank-math-link\">Lists<\/a>, <a href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple\" class=\"rank-math-link\">Tuples<\/a>, <a href=\"https:\/\/www.askpython.com\/python\/string\/python-string-functions\" class=\"rank-math-link\">Strings<\/a>, etc.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>slice(Stop)\nslice(Start, Stop[, Step)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Start<\/strong>: (Optional) An integer that specifies the index to initiate the slicing process.<\/li><li><strong>Stop<\/strong>: An integer that specifies the end index to the slice() method.<\/li><li><strong>Step<\/strong>: (Optional) An integer that specifies the step of the slicing process.<\/li><\/ul>\n\n\n\n<p><strong>Value returned by the slice() function:<\/strong><\/p>\n\n\n\n<p>A sliced object.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Understanding of slice() function<\/h2>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;Printing arguments passed to the slice().... &quot;)\ninput = slice(4)  \nprint(input.start)\nprint(input.stop)\nprint(input.step)\n\ninput = slice(1,4,6)  \nprint(input.start)\nprint(input.stop)\nprint(input.step)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Printing arguments passed to the slice().... \nNone\n4\nNone\n1\n4\n6<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python slice() with Strings<\/h2>\n\n\n\n<p>Python slice() function can be used along with Strings in two different ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>slice() function with positive indexes<\/em><\/li><li><em>slice() function with negative indexes<\/em><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1. slice() function with positive indexes<\/h3>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninput=&#039;Engineering&#039;\nresult=input&#x5B;slice(1,6)]\nprint(result)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ngine<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. slice() function with negative indexes<\/h3>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninput=&#039;Engineering&#039;\nresult=input&#x5B;slice(-5,-1)]\nprint(result)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>erin<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python slice() with Lists<\/h2>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninput_list = slice(1, 5) \nmy_list = &#x5B;&#039;Safa&#039;, &#039;Aman&#039;, &#039;Raghav&#039;, &#039;Raman&#039;, &#039;JournalDev&#039;, &#039;Seema&#039;]\nprint(my_list&#x5B;input_list])\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>['Aman', 'Raghav', 'Raman', 'JournalDev']<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python slice() with Tuples<\/h2>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninput_tuple = slice(1, 5)  \nmy_tuple = &#x5B;&#039;Safa&#039;, &#039;Aman&#039;, &#039;Raghav&#039;, &#039;Raman&#039;, &#039;JournalDev&#039;, &#039;Seema&#039;]\nprint(my_tuple&#x5B;input_tuple])\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>['Aman', 'Raghav', 'Raman', 'JournalDev']<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Extended indexing with Python slice()<\/h2>\n\n\n\n<p>A <em>shorthand method<\/em> can be used to serve the functionality of Python slice().<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>input[start:stop:step]<\/code><\/pre>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_tuple = &#x5B;&#039;Safa&#039;, &#039;Aman&#039;, &#039;Raghav&#039;, &#039;Raman&#039;, &#039;JournalDev&#039;, &#039;Seema&#039;]\nresult = my_tuple&#x5B;1:3] \nprint(result)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>['Aman', 'Raghav']<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Deletion of Python Slices<\/h2>\n\n\n\n<p>The <strong>del keyword<\/strong> can be used to delete the applied Slicing on a particular input element.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_tuple = &#x5B;&#039;Safa&#039;, &#039;Aman&#039;, &#039;Raghav&#039;, &#039;Raman&#039;, &#039;JournalDev&#039;, &#039;Seema&#039;]\n\ndel my_tuple&#x5B;:2]\nprint(my_tuple)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>['Raghav', 'Raman', 'JournalDev', 'Seema']<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Thus, in this article, we have understood the basic functionality of Python slice() function.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.python.org\/3\/c-api\/slice.html\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Python slice() documentation<\/a><\/li><li>Python slice() function<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python slice() function returns a sliced object from the set of indexes of the input specified by the user in accordance with the arguments passed to it. Thus, it enables the user to slice any sequence such as Lists, Tuples, Strings, etc. Syntax: Start: (Optional) An integer that specifies the index to initiate the slicing [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-2794","post","type-post","status-publish","format-standard","hentry","category-built-in-methods"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2794"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2794\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}