{"id":4412,"date":"2024-09-06T06:40:29","date_gmt":"2024-09-06T06:40:29","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=4412"},"modified":"2026-02-05T09:11:06","modified_gmt":"2026-02-05T09:11:06","slug":"how-slicing-in-python-works","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/","title":{"rendered":"How Slicing in Python Works"},"content":{"rendered":"\n<p>Python is a popular programming language that supports a vast range of data types such as strings, arrays, tuples and lists. Often python programmers need to be able to access specific parts of data to extract or modify them. This is where slicing is useful. Python Slicing allows you to effortlessly modify strings, lists tuples, and arrays using indexes. In this article, we will learn everything about slicing &#8211; what is slicing, how slicing works and some common examples.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#Indexing_in_Python\" >Indexing in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#Slicing_in_Python\" >Slicing in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#How_Slicing_Works_in_Python\" >How Slicing Works in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#1_Using_Index\" >1. Using Index<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#2_Using_slice_function\" >2. Using slice() function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#3_Using_islice_function\" >3. Using islice() function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#Slicing_Examples\" >Slicing Examples<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#1_Slice_Insert_Data\" >1. Slice Insert Data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#2_Slice_Modify_Data\" >2. Slice Modify Data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#4_Extract_Substring_Using_Slicing\" >4. Extract Substring Using Slicing<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#5_Reverse_String_with_Slicing\" >5. Reverse String with Slicing<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#6_Filter_List_in_Python\" >6. Filter List in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#7_Using_Negative_Indexes\" >7. Using Negative Indexes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Indexing_in_Python\"><\/span>Indexing in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before we understand slicing, it is essential to learn about indexing in Python. Slicing completely depends on indexing in Python. Index is the position of a character or element in an indexable data type such as list, array, tuple, or string. It is automatically assigned by Python when you define a data variable. It cannot be explicitly assigned or modified by us. <\/p>\n\n\n\n<p>Indexes can be positive or negative. The positive index begin from 0 assigned to the first character, 1 to next character and so on. The negative indexes start from 0 for the last character, -1 for the penultimate character and so on. Positive and negative indexes allow you to access elements from start or the end respectively. In fact, every character of a data can be accessed using both positive as well as negative indexes. Here is an example of positive as well as negative indexes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> 0  1  2  3 4  # positive index<br> h  e  l  l o  # string characters<br>-5 -4 -3 -2 -1  # negative index<\/pre>\n\n\n\n<p>As the data is modified the element indexes are also modified. So they are dynamic. For example, if you remove the first character of the above string, the indexes will become different for the other characters.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> 0  1  2 3  # positive index<br> e  l  l o  # string characters<br>-4 -3 -2 -1  # negative index<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Slicing_in_Python\"><\/span>Slicing in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now that we have a good understanding of indexing in Python, let us learn how to use it for slicing. Slicing is basically accessing one or group of elements in an iterable data such as string, array, list or tuple. It can be to insert, modify, or delete these elements. There are several ways to slice data in Python. Bu they all use 3 main parameters &#8211; start, stop and step. Start is the starting index of a slice. Stop is the ending index of the slice. Step is a parameter that means the number of elements to be skipped at a time, between start and stop indexes. Each of these parameters is optional and we will see how to use them below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_Slicing_Works_in_Python\"><\/span>How Slicing Works in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are 3 different ways to slice in Python<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_Index\"><\/span>1. Using Index<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We typically use index to access a single element from a list or array. Here is an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data = 'hello'<br>&gt;&gt;&gt; data[1]<br>'e'<\/pre>\n\n\n\n<p>Extending this approach, you can also extract a slice of a list. Here is the syntax to do this.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data[start:stop:step]<\/pre>\n\n\n\n<p>In the above code, we specify the start index, end index and step. Each of these are optional. Therefore, here are several simple ways to slice your string.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data[start:stop]         # from start to stop-1<br>data[start:]             # start till end<br>data[:stop]              # from first item to stop-1<br>data[:]                  # copy of whole array<br>data[start:stop:step]    # start to stop, skipping step each time<\/pre>\n\n\n\n<p>Here are some examples to demonstrate it. Let us say you have the following string.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = 'hello'<\/pre>\n\n\n\n<p>Here are the different slices, using the previously mentioned syntaxes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data[2:3] # start at index=2 and stop and index=3<br>'l'<br>&gt;&gt;&gt; data[2:]  # from index=2 till end<br>'llo'<br>&gt;&gt;&gt; data[:3]  # from start till index=3<br>'hel'<br>&gt;&gt;&gt; data[:] # copy full array<br>'hello'<br>&gt;&gt;&gt; data[1:4:2] # start at index=1 and stop and index=4 with step=2<br>'el'<\/pre>\n\n\n\n<p>You can also use negative indexes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data[-4:-2]<br>'el'<\/pre>\n\n\n\n<p>You can also use negative step as shown below. When the start index is after the stop index, with a negative index, then Python will traverse the string in reverse order. Here -1 is penultimate character and -4 is the first character, and step=-2 so Python starts displaying slice characters in reverse order.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">>>> data[-1:-4:-2]<br>'ol'<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_slice_function\"><\/span>2. Using slice() function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python also provides a constructor slice() to slice a sequence of characters. It also requires the same 3 arguments used above &#8211; start, stop and step. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">slice(start, stop, step)<\/pre>\n\n\n\n<p>You need to assign it to a variable and then use it use it directly inside [&#8230;]. Here are both examples for your reference.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data='hello'\n\n&gt;&gt;&gt; x=slice(2,3)\n&gt;&gt;&gt; data[x]\n'l'\n\n&gt;&gt;&gt; data[slice(2,3)]\n'l'<\/pre>\n\n\n\n<p>Let us look at the above examples using slice() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data[slice(2,3)] # from index=2 to index=3<br>'l'<br>&gt;&gt;&gt; data[slice(2,)] # from index=2 till end<br>'he'<br>&gt;&gt;&gt; data[slice(2)] # from index=2 till end<br>'he'<br>&gt;&gt;&gt; data[slice(0,3)] # from index=0 till index=3<br>'hel'<br>&gt;&gt;&gt; data[slice(0,4,2)] # from index=0 to index=4 with step=2<br>'hl'<\/pre>\n\n\n\n<p>As you can see, slice() offers numerous ways to extract data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_islice_function\"><\/span>3. Using islice() function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Both the above approaches return a slice object of items. But what if your slice is very large? It will occupy a lot of memory space. In such cases, it is advisable to use islice() function.<\/p>\n\n\n\n<p>Python&#8217;s itertools library also provides a built-in function islice() that has same syntax as slice() function, but returns an iterable instead of the slice object. This is useful if you have a large iterable and want to extract a slice iterable, that does not occupy too much memory.<\/p>\n\n\n\n<p>Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">itertools.islice(iterable, start, end, step)<\/pre>\n\n\n\n<p>Here is an example to demonstrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; a=itertools.islice(data,1,3)<br>&gt;&gt;&gt; a<br>&lt;itertools.islice object at 0x032D93F0&gt;<br>&gt;&gt;&gt; print(a)<br>&lt;itertools.islice object at 0x032D93F0&gt;<\/pre>\n\n\n\n<p>As you can see above, islice() returns an iterable and not a slice object. To display its contents, we use join() function as shown below. It will iterate through the iterable and concatenate its elements to form the slice string.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; ''.join(a)<br>'el'<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Slicing_Examples\"><\/span>Slicing Examples<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Generally, developers use slicing for simply accessing and extracting a substring from a larger string. But there are many more things that you can do with slicing. Now let us learn some of the common use cases helpful for Python developers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Slice_Insert_Data\"><\/span>1. Slice Insert Data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use slicing to insert data to a list, array or tuple. Insertion can be done at the start or end of the data as shown below. Here is an example to insert character at the start of a list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data=['h','e','l','l','o']<br>&gt;&gt;&gt; data[:0]='s'<br>&gt;&gt;&gt; data<br>['s', 'h', 'e', 'l', 'l', 'o']<\/pre>\n\n\n\n<p>Here is an example to insert character at the end of the list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data<br>['s', 'h', 'e', 'l', 'l', 'o']<br>&gt;&gt;&gt; data[len(data):]='s'<br>&gt;&gt;&gt; data<br>['s', 'h', 'e', 'l', 'l', 'o', 's']<\/pre>\n\n\n\n<p>Please note, this does not work on a string since Python <a href=\"https:\/\/developers.google.com\/edu\/python\/strings\" target=\"_blank\" rel=\"noreferrer noopener\">strings are immutable<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">>>> data='hello'<br>>>> data[:0]='s'<br>Traceback (most recent call last):<br>  File \"&lt;stdin>\", line 1, in &lt;module><br>TypeError: 'str' object does not support item assignment<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Slice_Modify_Data\"><\/span>2. Slice Modify Data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Using the above example, you can easily modify specific characters using indexes. Here is the example to modify middle 3 characters.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data=['h','e','l','l','o']<br>&gt;&gt;&gt; data[1:4]=['g','g','g']<br>&gt;&gt;&gt; data<br>['h', 'g', 'g', 'g', 'o']<\/pre>\n\n\n\n<p>In the above example, we have used indexes 1 to 4 to select our slice and then replaced it with a new list [&#8216;g&#8217;,&#8217;g&#8217;,&#8217;g&#8217;].<\/p>\n\n\n\n<p>You can also use the same method to insert data at a specific point.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data=['h','e','l','l','o']<br>&gt;&gt;&gt; data[4:4]=['g','g','g']<br>&gt;&gt;&gt; data<br>['h', 'e', 'l', 'l', 'g', 'g', 'g', 'o']<\/pre>\n\n\n\n<p>In the above example, we have used 4:4 slice to indicate we are inserting data at index=4 position. We have inserted list [&#8216;g&#8217;,&#8217;g&#8217;,&#8217;g&#8217;] at that position.<\/p>\n\n\n\n<p>Please note, strings do not support modification. You can use it for lists, arrays and tuples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Extract_Substring_Using_Slicing\"><\/span>4. Extract Substring Using Slicing<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This is the simplest use case to extract substring from a string using indexes. Here are some of the many ways to easily extract substring. We have already covered this earlier.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data=['h','e','l','l','o']<br>&gt;&gt;&gt; data[2:3]<br>['l']<br><br>&gt;&gt;&gt; data[3:]<br>['l', 'o']<br><br>&gt;&gt;&gt; data[:2]<br>['h', 'e']<br><br>&gt;&gt;&gt; data[0:4:2]<br>['h', 'l']<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Reverse_String_with_Slicing\"><\/span>5. Reverse String with Slicing<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This is perhaps the most interesting use case of slicing. Typically, people think that slicing means extracting one or more characters from a larger string or list. But being able to completely reverse the string or list is something amazing. For this, all you need to do is omit start and stop indexes and use a negative step=-1.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data=['h','e','l','l','o']<br>&gt;&gt;&gt; data[::-1]<br>['o', 'l', 'l', 'e', 'h']<\/pre>\n\n\n\n<p>The above method works on arrays, lists, strings and tuples since we are not modifying original data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_Filter_List_in_Python\"><\/span>6. Filter List in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use the step argument to filter specific items from a sequence of characters. Here is an example to extract the odd characters in list. We use step=2 to skip every other item.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data[::2]<br>['h', 'l', 'o']<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7_Using_Negative_Indexes\"><\/span>7. Using Negative Indexes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>As mentioned earlier, you can also use negative indexes to access specific elements from lists, strings, arrays and tuples. Here are some examples to illustrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data=['h','e','l','l','o']<br><br>&gt;&gt;&gt; data[-3:-1]<br>['l', 'l']<br><br>&gt;&gt;&gt; data[-3:]<br>['l', 'l', 'o']<br><br>&gt;&gt;&gt; data[:-1]<br>['h', 'e', 'l', 'l']<br><br>&gt;&gt;&gt; data[-3:-1:2]<br>['l']<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Slicing is a very convenient and powerful way to manipulate indexable data types in Python. It can be used to write concise and clear code. It will save you a lot of development time. Plus it is superfast in performance too. In this article, we have learnt how to slice strings, lists, arrays and tuples in Python. We learnt what is indexing, the different ways to perform slicing and also went through some common use cases. They all work with most Python versions. However, it is important to remember that most slicing operations work with all iterable data like strings, tuples, arrays and lists, except those operations that modify the data. Since strings are immutable, you cannot modify them using slicing operator.<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/\">Ubiq<\/a>&nbsp;makes it easy to visualize data, and monitor them in real-time dashboards.&nbsp;<a href=\"https:\/\/ubiq.co\/accounts\/register\">Try Ubiq<\/a>&nbsp;for free.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-ask-user-input-in-python-until-valid-response\/\">How to Ask User Input in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-bind-events-to-dynamically-created-elements-in-javascript\/\">How to Bind Events to Dynamically Created Elements in JavaScript<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-password-protect-directory-in-nginx\/\">How to Password Protect Directory in NGINX<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Slicing is an effective way to manipulate strings and arrays. Let us learn more about how slicing works in Python.<\/p>\n","protected":false},"author":1,"featured_media":4439,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[304,303],"class_list":["post-4412","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-index","tag-slicing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How Slicing in Python Works - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Python Slicing is an effective way to manipulate strings and arrays. Let us learn more about how slicing works in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How Slicing in Python Works - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Python Slicing is an effective way to manipulate strings and arrays. Let us learn more about how slicing works in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2024-09-06T06:40:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-05T09:11:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/09\/slicing-in-python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"602\" \/>\n\t<meta property=\"og:image:height\" content=\"402\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-slicing-in-python-works\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-slicing-in-python-works\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How Slicing in Python Works\",\"datePublished\":\"2024-09-06T06:40:29+00:00\",\"dateModified\":\"2026-02-05T09:11:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-slicing-in-python-works\\\/\"},\"wordCount\":1341,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-slicing-in-python-works\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/09\\\/slicing-in-python.jpg?fit=602%2C402&ssl=1\",\"keywords\":[\"index\",\"slicing\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-slicing-in-python-works\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-slicing-in-python-works\\\/\",\"name\":\"How Slicing in Python Works - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-slicing-in-python-works\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-slicing-in-python-works\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/09\\\/slicing-in-python.jpg?fit=602%2C402&ssl=1\",\"datePublished\":\"2024-09-06T06:40:29+00:00\",\"dateModified\":\"2026-02-05T09:11:06+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Python Slicing is an effective way to manipulate strings and arrays. Let us learn more about how slicing works in Python.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-slicing-in-python-works\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-slicing-in-python-works\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-slicing-in-python-works\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/09\\\/slicing-in-python.jpg?fit=602%2C402&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/09\\\/slicing-in-python.jpg?fit=602%2C402&ssl=1\",\"width\":602,\"height\":402,\"caption\":\"slicing in python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-slicing-in-python-works\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How Slicing in Python Works\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How Slicing in Python Works - Ubiq BI","description":"Python Slicing is an effective way to manipulate strings and arrays. Let us learn more about how slicing works in Python.","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:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/","og_locale":"en_US","og_type":"article","og_title":"How Slicing in Python Works - Ubiq BI","og_description":"Python Slicing is an effective way to manipulate strings and arrays. Let us learn more about how slicing works in Python.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-09-06T06:40:29+00:00","article_modified_time":"2026-02-05T09:11:06+00:00","og_image":[{"width":602,"height":402,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/09\/slicing-in-python.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How Slicing in Python Works","datePublished":"2024-09-06T06:40:29+00:00","dateModified":"2026-02-05T09:11:06+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/"},"wordCount":1341,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/09\/slicing-in-python.jpg?fit=602%2C402&ssl=1","keywords":["index","slicing"],"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/","url":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/","name":"How Slicing in Python Works - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/09\/slicing-in-python.jpg?fit=602%2C402&ssl=1","datePublished":"2024-09-06T06:40:29+00:00","dateModified":"2026-02-05T09:11:06+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Python Slicing is an effective way to manipulate strings and arrays. Let us learn more about how slicing works in Python.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/09\/slicing-in-python.jpg?fit=602%2C402&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/09\/slicing-in-python.jpg?fit=602%2C402&ssl=1","width":602,"height":402,"caption":"slicing in python"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-slicing-in-python-works\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How Slicing in Python Works"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/09\/slicing-in-python.jpg?fit=602%2C402&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-19a","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/4412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=4412"}],"version-history":[{"count":40,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/4412\/revisions"}],"predecessor-version":[{"id":10518,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/4412\/revisions\/10518"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/4439"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=4412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=4412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=4412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}