I see, learn and rediscoverโ€ฆ everyday!
 
Slice…

Slice…

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’m not very sure whether it can be used for other data types.

For a string, it can be used as variable_name[start:end:step]

If the start and end are equal, then a null string is returned.
If step is positive and start is greater than the end, then a null string is returned.
If step is negative and start is lesser than the end, then a null string is returned.
Ok. If you are confused about start, end, step, equal, greater. lesser, positive and negative, don’t worry. Things are explained very clearly after this. ๐Ÿ™‚

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.

Let us see a simple slice example.

>>> i = “Hello World”
>>> i[2:10:1]
‘llo Worl’

So it’s very clear that the basic action of the slice notation is to return a sub string.
In the above example, it returns the characters from the string whose position are equal to and greater than start and less than end.
Note :: The character at position end is not returned.
In the above example i[10] = “d” is not returned.

If the start is not specified and step is positive, then the default value is 0
If the end is not specified and step is positive, then the default value is len(string).
If the start is not specified and step is negative then the default value is len(string).
If 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.
If the step is not specified, then the default value is 1

So i[::] prints the entire string. In fact, in most cases the step is not specified. So i[:] returns the entire string.

>>> i = “Hello World”
>>> i[::]
‘Hello World’
>>> i[:]
‘Hello World’
>>>

Slice notation can also take negative values.
If the start value is negative, then the last start characters are returned.
If the end value is negative, then the last end characters are not returned.

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.

>>> i = “Hello World”
>>> i[-5:] #Start is -ve. So last 5 characters
‘World’
>>> i[:-5] #End is -ve. So everything except last 5 characters
‘Hello ‘
>>> i[-8:-3] #Start is -8. =>”lo World”. In this don’t include last 3 characters => “lo Wo”
‘lo Wo’
>>>

The real fun is when even step is negative.
If the start is greater than the end, then the step must be -ve, which makes the string to be returned as reverse.

So consider this example.
>>> i = “Hello World”
>>> i[8:4:-1]
‘roW ‘
>>>

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.

Finally, if step is something other than 1, then every stepth character is returned. For example
>>> i = “Hello World”
>>> i[::2]
‘HloWrd’
>>> i[::-2]
‘drWolH’
>>>

Any idea why i wrote this post.

Saw this small snippet for string reverse in python

>>> a = “Hello World”
>>> print a[::-1]
dlroW olleH
>>>

Sweet and cute na. ๐Ÿ˜‰

To know more about slice notation you refer to the python python doc.

6 Comments

  1. Harishankaran

    you people are hopeless !!!
    I’ve written a nice tutorial. Looks like no one has even saw that ๐Ÿ˜› All you saw is “sweet and cute” ๐Ÿ˜›

    And yeah the code is “sweet and cute”. Hmmmm you may call it sexy if you want, but i prefer sweet and cute ๐Ÿ˜‰

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.