Slicing Strings
Life with Python #4
I am back with some more string theory! It turns out that in python, we can get a specific part (or “slice”) of a string using the following syntax:
string[start:stop:step]The above code will return a slice of the given string based on the following arguments.
start: The index where the slice begins (included in the slice). It is optional, and if omitted, slicing starts from the beginning of the givenstringi.e. the default value is0representing the first position.stop: The index where the slice ends (however, it is not included in the slice). If omitted, slicing goes to the end of the string i.e. the default value passed islen(string)-1which will be the last index of the givenstring.step: The interval at which characters are taken. If omitted, it defaults to 1 (each character). If it’s negative, it allows slicing in reverse.
It is instructive to look at some examples to really come to grips with the power and utility of slicing. So, let’s look at different ways to slice a string.
Suppose you have a string stored as follows:
string1 = "Hello, World!"Basic Slicing
string1[1:5] # 'ello'
string1[:5] # 'Hello'
string1[7:] # 'World!'What if we are using negative indices? A negative index counts from the end of the string.
string1[-6:] # 'World!'Here, -6 means six characters from the end ie it starts the slicing from W all the way to the end.
string1[:-6] # 'Hello, '
string1[1:-8] # 'ello'Here, in the first line, we have given the stopindex as -6 therefore, it will slice upto W from the beginning of the string. And in the second line, we have given thestartindex as 1 and stopindex as -8 (corresponding to the 8-th character from the end- therefore, it will slice starting from e upto ,.
Get Prathik C’s stories in your inbox
Join Medium for free to get updates from this writer.
Step Value Slices
Consider a new string as follows:
string2 = "0123456789"The most straightforward use case is as follows:
string2[::2] # '02468'
string2[3::2] # '3579'
string2[4:8:3] # '47'In the first line, it simply returns every second character from the whole string. In the second line, it returns every second character starting from the third index till the last. In the third line, it returns every third character starting from 4th index upto 7th index.
Now how about negative steps? So it turns out we can have negative steps (or reverse slicing) as well. The nicest use case is how we can reverse a string.
string2[::-1] # '9876543210'
string2[::-2] # '97531'
string2[:3:-3] # '96'
string2[8::-3] # '852'
string2[-1:-5:-3] # '96'The first line reverses the entire string by taking each characters from end to start. The second line returns every second character starting from the end of the string. The third line returns every third character starting from the end of the string upto the 3rd index. The fourth line returns every third character starting from the 8th index and moving to the beginning of the string. The fifth line returns every third character starting from the last index (represented by -1)and moving upto the 5th index from the end of the string (represented by -5).
Here are some things to keep in mind:
- If
startis greater thanstopandstepis positive, you get an empty string. For example,string1[5:1:1]will return''(empty string). Similarly,string2[-2:-8:1]will also return''. - Omitting all arguments (
string1[:]orstring2[::]) returns a copy of the string.
So that covers the basics of slicing. Have fun being a string ninja! Okay, I apologize for that bad joke. [:P] Until next time!










