{"id":74,"date":"2007-09-20T01:42:00","date_gmt":"2007-09-20T08:42:00","guid":{"rendered":"http:\/\/sp2hari.com\/?p=74"},"modified":"2020-06-20T08:56:25","modified_gmt":"2020-06-20T08:56:25","slug":"slice","status":"publish","type":"post","link":"https:\/\/sp2hari.com\/index.php\/2007\/09\/20\/slice\/","title":{"rendered":"Slice&#8230;"},"content":{"rendered":"<div style=\"text-align: justify;\">Slice notation is one of the powerful feature in python using which can be used for operations like substr, str_reverse etc etc.<\/div>\n<div style=\"text-align: justify;\">The slice notation is really powerful for strings. I&#8217;m not very sure whether it can be used for other data types.<\/p>\n<p>For a string, it can be used as variable_name[start:end:step]<\/p>\n<\/div>\n<div style=\"text-align: justify;\">If the start and end are equal, then a null string is returned.<br \/>\nIf step is positive and  start is greater than the end, then a null string is returned.<br \/>\nIf step is negative and start is lesser than the end, then a null string is returned.<\/div>\n<div style=\"text-align: justify;\">Ok. If you are confused about start, end, step, equal, greater. lesser, positive and negative, don&#8217;t worry. Things are explained very clearly after this. \ud83d\ude42<\/p>\n<div style=\"text-align: justify;\">First few lines about the step. If the step is positive, then the direction in which the input string is scanned is forward direction. If the step  is negative, then the string is scanned in reverse direction. So now i hope you understand why start must be less than end if step is positive. For the same reason, start must be less than end if the step is negative.<\/div>\n<\/div>\n<p>Let us see a simple slice example.<\/p>\n<p><span style=\"font-family:courier new;\">&gt;&gt;&gt; i = &#8220;Hello World&#8221;<br \/>\n<\/span><span style=\"font-family:courier new;\">&gt;&gt;&gt; i[2:10:1]<br \/>\n<\/span><span style=\"font-family:courier new;\">&#8216;llo Worl&#8217;<\/span><\/p>\n<div style=\"text-align: justify;\">So it&#8217;s very clear that the basic action of the slice notation is to return a sub string.<br \/>\nIn the above example, it returns the characters from the string whose position are equal to and greater than start and less than end.<\/div>\n<div style=\"text-align: justify;\">Note :: The character at position end is not returned.<br \/>\nIn the above example i[10] = &#8220;d&#8221; is not returned.<\/div>\n<p>If the start is not specified and step is positive, then the default value is 0<br \/>\nIf the end is not specified and step is positive, then the default value is len(string).<br \/>\nIf the start is not specified and step is negative then the default value is len(string).<br \/>\nIf the end is not specified and step is negative, then the default value is NOT 0, coz in this case even 0th character is returned.<br \/>\nIf the step is not specified, then the default value is 1<\/p>\n<p>So i[::] prints the entire string. In fact, in most cases the step is not specified. So i[:] returns the entire string.<\/p>\n<p><span style=\"font-family:courier new;\">&gt;&gt;&gt; i = &#8220;Hello World&#8221;<br \/>\n<\/span><span style=\"font-family:courier new;\">&gt;&gt;&gt; i[::]<br \/>\n<\/span><span style=\"font-family:courier new;\">&#8216;Hello World&#8217;<br \/>\n<\/span><span style=\"font-family:courier new;\">&gt;&gt;&gt; i[:]<br \/>\n<\/span><span style=\"font-family:courier new;\">&#8216;Hello World&#8217;<br \/>\n<\/span><span style=\"font-family:courier new;\">&gt;&gt;&gt;<br \/>\n<\/span><br \/>\nSlice notation can also take negative values.<br \/>\nIf the start value is negative, then the last start characters are returned.<br \/>\nIf the end value is negative, then the last end characters are not returned.<\/p>\n<div style=\"text-align: justify;\">So if both start and end values are negative, then the absolute value of start must be greater than the absolute value of end. Else a null string will be returned.<\/div>\n<p><span style=\"font-family:courier new;\">&gt;&gt;&gt; i = &#8220;Hello World&#8221;<br \/>\n<\/span><span style=\"font-family:courier new;\">&gt;&gt;&gt; i[-5:]             #Start is -ve. So last 5 characters<br \/>\n<\/span><span style=\"font-family:courier new;\">&#8216;World&#8217;<br \/>\n<\/span><span style=\"font-family:courier new;\">&gt;&gt;&gt; i[:-5]            #End is -ve. So everything except last 5 characters<\/span><br \/>\n<span style=\"font-family:courier new;\">&#8216;Hello &#8216;<\/span><br \/>\n<span style=\"font-family:courier new;\">&gt;&gt;&gt; i[-8:-3]       #Start is -8. =&gt;&#8221;lo World&#8221;. In this don&#8217;t include last 3 characters =&gt; &#8220;lo Wo&#8221;<br \/>\n<\/span><span style=\"font-family:courier new;\">&#8216;lo Wo&#8217;<\/span><br \/>\n<span style=\"font-family:courier new;\">&gt;&gt;&gt;<br \/>\n<\/span><\/p>\n<div style=\"text-align: justify;\">The real fun is when even step is negative.<br \/>\nIf the start is greater than the end, then the step must be -ve, which makes the string to be returned as reverse.<\/div>\n<p>So consider this example.<br \/>\n<span style=\"font-family:courier new;\">&gt;&gt;&gt; i = &#8220;Hello World&#8221;<br \/>\n<\/span><span style=\"font-family:courier new;\">&gt;&gt;&gt; i[8:4:-1]<br \/>\n<\/span><span style=\"font-family:courier new;\">&#8216;roW &#8216;<br \/>\n<\/span><span style=\"font-family:courier new;\">&gt;&gt;&gt; <\/span><\/p>\n<div style=\"text-align: justify;\">In the above example, i[8:4] part refers to i[8] and i[4] positions and since the step is negative, now it returns characters at i[8], i[7], i[6], i[5]. Note that even here i[4] is not returned.<\/div>\n<p>Finally, if step is something other than 1, then every stepth character is returned.  For example<br \/>\n&gt;&gt;&gt; i = &#8220;Hello World&#8221;<br \/>\n&gt;&gt;&gt; i[::2]<br \/>\n&#8216;HloWrd&#8217;<br \/>\n&gt;&gt;&gt; i[::-2]<br \/>\n&#8216;drWolH&#8217;<br \/>\n&gt;&gt;&gt;<\/p>\n<p>Any idea why i wrote this post.<\/p>\n<p>Saw this small snippet for string reverse in python<\/p>\n<p><span style=\"font-family:courier new;\">&gt;&gt;&gt; a = &#8220;Hello World&#8221;<br \/>\n<\/span><span style=\"font-family:courier new;\">&gt;&gt;&gt; print a[::-1]<br \/>\n<\/span><span style=\"font-family:courier new;\">dlroW olleH<br \/>\n<\/span><span style=\"font-family:courier new;\">&gt;&gt;&gt;<br \/>\n<\/span><br \/>\nSweet and cute na. \ud83d\ude09<\/p>\n<p>To know more about slice notation you refer to the python <a href=\"http:\/\/docs.python.org\/tut\/node5.html#SECTION005120000000000000000\">python doc<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Slice notation is one of the powerful feature in python using which can be used for operations like substr, str_reverse etc etc. The slice notation is really powerful for strings. I&#8217;m not very sure whether it can be used for other data types. For a string, it can be used &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":2,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":1438,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/74\/revisions\/1438"}],"wp:attachment":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}