{"id":78259,"date":"2020-04-11T17:41:16","date_gmt":"2020-04-11T12:11:16","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=78259"},"modified":"2020-04-11T17:41:16","modified_gmt":"2020-04-11T12:11:16","slug":"python-slicing","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/","title":{"rendered":"Slicing in Python with Python Slice Function and Indexing"},"content":{"rendered":"<p>In this article, we will learn all about <strong>Slicing in Python<\/strong>. We\u2019ll learn what slicing <strong>means<\/strong> and the <strong>different<\/strong> ways in which we can create slices. We\u2019ll also learn to apply slicing on <strong>strings<\/strong>, <strong>tuples<\/strong>, and <strong>lists<\/strong> through various examples ahead.<\/p>\n<p>So stick along till the end of this article.<\/p>\n<h3>What is Slicing in Python?<\/h3>\n<p>Slicing, as its name suggests is the process of <strong>accessing<\/strong> a particular <strong>piece<\/strong> of a <strong>sequence<\/strong>. It is a feature in Python that lets you access parts of sequences like <strong>strings<\/strong>, <strong>tuples<\/strong>, and <strong>lists<\/strong>. These <strong>parts<\/strong> that you get <strong>after slicing<\/strong> are known as <strong>slices<\/strong>.<\/p>\n<p>Also, slices are <strong>non-destructive<\/strong>. This means that accessing a slice <strong>doesn\u2019t change<\/strong> the <strong>original<\/strong> <strong>value<\/strong> of the sequence.<\/p>\n<p>But you can <strong>modify mutable<\/strong> <strong>sequences<\/strong> like lists by assigning <strong>new values<\/strong> to its slice(s).<\/p>\n<p>Slicing in Python can be done using 2 methods:<\/p>\n<ol>\n<li>Using <strong>indexing<\/strong> to create slices.<\/li>\n<li>Using the <strong>slice() function<\/strong> to create a <strong>slice object<\/strong> for slicing.<\/li>\n<\/ol>\n<p>Before we learn how to work with these two python methods, we need to understand <strong>Python\u2019s slice notation.<\/strong><\/p>\n<h3>Understanding slice notation: start, stop and step<\/h3>\n<p>The slice notation is a <strong>pre-requisite<\/strong> when it comes to implementing <strong>slicing<\/strong> in Python. Let\u2019s see what start, stop and step mean.<\/p>\n<p>1. The <strong>start<\/strong> value is an integer which indicates the starting index value.<\/p>\n<p>2. The <strong>stop<\/strong> value indicates the index value to stop at. The element at this index is not included in the slice.<\/p>\n<p>3. The <strong>step<\/strong> value refers to the number of steps(indices) to jump while going from start to stop.<\/p>\n<p>Both the slicing methods discussed above make use of the <strong>slice notation<\/strong>.<\/p>\n<p>Let\u2019s see how!<\/p>\n<h3>Indexing to create Python slices<\/h3>\n<p>We are familiar with the square bracket notation used to access elements in a sequence through their <strong>indices<\/strong>.<\/p>\n<p>Let\u2019s look at some examples while working at the shell:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; mystring = \"techvidvan\"\n&gt;&gt;&gt; mystring[0]\n't'\n&gt;&gt;&gt; mystring[-1]\n'n'\n&gt;&gt;&gt;<\/pre>\n<p>Python supports accessing values of a <strong>sequence<\/strong> using both <strong>positive<\/strong> and <strong>negative indexing<\/strong>. <strong>Positive index<\/strong> values <strong>count<\/strong> from <strong>left<\/strong> <strong>to<\/strong> <strong>right<\/strong>, whereas <strong>negative index<\/strong> values count from <strong>right<\/strong> <strong>to<\/strong> <strong>left<\/strong>.<\/p>\n<p>So, in our example, <strong>mystring[0]<\/strong> gives the element at index <strong>0<\/strong>, i.e, the <strong>first element<\/strong>. And <strong>mystring[-1]<\/strong> gives the element at index <strong>-1<\/strong>, i.e, the <strong>last element<\/strong>.<\/p>\n<p><strong>This square bracket notation can be extended to access slices of a sequence. <\/strong>This is done by putting the slice notation inside the <strong>square brackets<\/strong>.<\/p>\n<p>The <strong>start, stop and step<\/strong> values are separated from one another by the colon (:) character as: <strong>mystring[start:stop:step]<\/strong><\/p>\n<p>These <strong>three values<\/strong> are optional when used together:<\/p>\n<p>1. When <strong>start<\/strong> value is missing, it has a default value 0.<\/p>\n<p>2. When <strong>stop<\/strong> value is missing, it takes the value of the length of the sequence. (This is the <strong>maximum<\/strong> value stop can have)<\/p>\n<p>3. When <strong>step<\/strong> is missing, it takes the default value of 1.<\/p>\n<p>Follow along through the <strong>Python shell session<\/strong> and look at the examples:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; digits = [10, 20, 30, 40]\n&gt;&gt;&gt; digits[:]\n[10, 20, 30, 40]\n&gt;&gt;&gt; digits[:3]\n[10, 20, 30]\n&gt;&gt;&gt; digits[2:]\n[30, 40]\n&gt;&gt;&gt; digits[::2]\n[10, 30]\n&gt;&gt;&gt; digits[-1: -4 : -1]\n[40, 30, 20]<\/pre>\n<h3>Using Python slice() function to create slices<\/h3>\n<p>We can use the <strong>slice() function<\/strong> to <strong>create slices<\/strong>. The slice function takes in the values of <strong>start<\/strong>, <strong>stop<\/strong> and <strong>step<\/strong> as <strong>arguments<\/strong> and <strong>returns<\/strong> a <strong>slice object<\/strong>. This slice object can then be passed within the<strong> square brackets<\/strong> in place of <strong>start:stop:step<\/strong> to access the <strong>desired slice<\/strong>.<\/p>\n<p>slice() accepts <strong>three parameters<\/strong>:<\/p>\n<p>1. When <strong>start<\/strong> is missing, it defaults to None.<\/p>\n<p><strong>2. stop<\/strong> can\u2019t be optional.<\/p>\n<p>3. When <strong>step<\/strong> is missing, it takes the default value of None.<\/p>\n<p>The steps involved while <strong>creating slices<\/strong> using a slice object:<\/p>\n<p>Step 1: Creating a slice object:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; slice_object = slice(1, 5, 2)\n&gt;&gt;&gt; print(slice_object)<\/pre>\n<p>Output: slice(1, 5, 2)<\/p>\n<p>Step 2: Using the slice object to <strong>access<\/strong> a slice:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; l = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n&gt;&gt;&gt; l[slice_object]<\/pre>\n<p>Output: [2, 6]<\/p>\n<p>Let\u2019s look at some more examples in the <strong>Python shell<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; l = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n&gt;&gt;&gt; slice_object = slice(5)\n&gt;&gt;&gt; l[slice_object]\n[0, 2, 4, 6, 8]\n&gt;&gt;&gt; slice_object = slice(3, 7)\n&gt;&gt;&gt; l[slice_object]\n[6, 8, 10, 12]\n&gt;&gt;&gt; slice_object = slice(2, 8, 2)\n&gt;&gt;&gt; l[slice_object]\n[4, 8, 12]\n&gt;&gt;&gt; l[slice(-1,-7,-2)]\n[18, 14, 10]<\/pre>\n<p>We saw how we can <strong>slice list<\/strong>.<\/p>\n<p>Now we\u2019ll look at examples for <strong>slicing tuples<\/strong> and <strong>strings<\/strong>. Make sure to follow along at <strong>your &gt;&gt;&gt; prompt<\/strong>, and experiment with slicing.<\/p>\n<h3>Slicing in python Tuples<\/h3>\n<p><strong>Tuples<\/strong> work very similar to <strong>lists<\/strong> when it comes to <strong>slicing<\/strong>. The only difference is that you can use <strong>slicing<\/strong> to <strong>modify<\/strong> <strong>slices<\/strong> of a <strong>list<\/strong> but you <strong>cannot<\/strong> <strong>modify tuples<\/strong> as they are <strong>immutable<\/strong>.<\/p>\n<ul>\n<li><strong>Using indexing<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; digits = (10, 20, 30, 40)\n&gt;&gt;&gt; digits[:]\n(10, 20, 30, 40)\n&gt;&gt;&gt; digits[:3]\n(10, 20, 30)\n&gt;&gt;&gt; digits[2:]\n(30, 40)\n&gt;&gt;&gt; digits[::2]\n(10, 30)\n&gt;&gt;&gt; digits[-1: -4 : -1]\n(40, 30, 20)<\/pre>\n<\/li>\n<li><strong>Using slice() function<\/strong>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; digits = (10, 20, 30, 40)\n&gt;&gt;&gt; digits[:]\n(10, 20, 30, 40)\n&gt;&gt;&gt; digits[:3]\n(10, 20, 30)\n&gt;&gt;&gt; digits[2:]\n(30, 40)\n&gt;&gt;&gt; digits[::2]\n(10, 30)\n&gt;&gt;&gt; digits[-1: -4 : -1]\n(40, 30, 20)<\/pre>\n<\/li>\n<\/ul>\n<h3>Slicing strings<\/h3>\n<p>A string is just a <strong>sequence<\/strong> of <strong>characters<\/strong>. We can use slicing to access <strong>parts<\/strong> of a <strong>string<\/strong>. We cannot <strong>modify<\/strong> <strong>slices<\/strong> of a string as <strong>strings <\/strong>are <strong>immutable<\/strong>.<\/p>\n<ul>\n<li><strong>Using Indexing<\/strong><\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; greet = \"Hello, world\"\n&gt;&gt;&gt; greet[:]\n'Hello, world'\n&gt;&gt;&gt; greet[::-1]\n'dlrow ,olleH'\n&gt;&gt;&gt; greet[2:6]\n'llo,'\n&gt;&gt;&gt; greet[::2]\n'Hlo ol'\n&gt;&gt;&gt; greet[2:11:2]\n'lo ol'<\/pre>\n<ul>\n<li><strong>Using slice() function<\/strong><\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; slice_obj = slice(6)\n&gt;&gt;&gt; greet[slice_obj]\n'Hello,'\n&gt;&gt;&gt; greet[slice(3,10)]\n'lo, wor'\n&gt;&gt;&gt; greet[slice(2,11,2)]\n'lo ol'\n&gt;&gt;&gt; greet[slice(-1, -13, -1)]\n'dlrow ,olleH'<\/pre>\n<h3>Wrapping Up!<\/h3>\n<p>In this article, we learned the feature of <strong>slicing<\/strong> and how <strong>handy<\/strong> and <strong>powerful<\/strong> it is. We also looked at the methods through which we can access slices of a <strong>sequence<\/strong>.<\/p>\n<p>We advise you to take some time and understand how these <strong>examples<\/strong> work by using them in your <strong>code<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn all about Slicing in Python. We\u2019ll learn what slicing means and the different ways in which we can create slices. We\u2019ll also learn to apply slicing on strings,&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":78267,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[2395,2396,2397,1155,2398,2399,2400,2401,2402],"class_list":["post-78259","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-indexing-to-create-python-slice","tag-python-slice","tag-python-slice-object","tag-python-slice-string","tag-python-slice-function","tag-python-slicing","tag-python-slicing-tuples","tag-slicing-in-python","tag-what-is-python-slice"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Slicing in Python with Python Slice Function and Indexing - TechVidvan<\/title>\n<meta name=\"description\" content=\"Slicing in python -Learn What is slicing, Slice notation:Start, stop and step,Indexing to create slices, Slice ()function, slicing tuples, slicing strings,.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-slicing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Slicing in Python with Python Slice Function and Indexing - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Slicing in python -Learn What is slicing, Slice notation:Start, stop and step,Indexing to create slices, Slice ()function, slicing tuples, slicing strings,.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-slicing\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-11T12:11:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-slicing-slice-function.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Slicing in Python with Python Slice Function and Indexing - TechVidvan","description":"Slicing in python -Learn What is slicing, Slice notation:Start, stop and step,Indexing to create slices, Slice ()function, slicing tuples, slicing strings,.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/","og_locale":"en_US","og_type":"article","og_title":"Slicing in Python with Python Slice Function and Indexing - TechVidvan","og_description":"Slicing in python -Learn What is slicing, Slice notation:Start, stop and step,Indexing to create slices, Slice ()function, slicing tuples, slicing strings,.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-04-11T12:11:16+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-slicing-slice-function.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Slicing in Python with Python Slice Function and Indexing","datePublished":"2020-04-11T12:11:16+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/"},"wordCount":775,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-slicing-slice-function.jpg","keywords":["Indexing to Create Python Slice","Python slice","Python Slice Object","Python Slice String","Python Slice() Function","Python Slicing","Python Slicing Tuples","Slicing in Python","What is Python Slice"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-slicing\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/","url":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/","name":"Slicing in Python with Python Slice Function and Indexing - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-slicing-slice-function.jpg","datePublished":"2020-04-11T12:11:16+00:00","description":"Slicing in python -Learn What is slicing, Slice notation:Start, stop and step,Indexing to create slices, Slice ()function, slicing tuples, slicing strings,.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-slicing\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-slicing-slice-function.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/python-slicing-slice-function.jpg","width":802,"height":420,"caption":"Slicing in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-slicing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Slicing in Python with Python Slice Function and Indexing"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=78259"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/78259\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/78267"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=78259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=78259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=78259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}