Python Slicing Exercises
Let’s check out some exercises that will help you understand and master how Python’s Slice Notions, Slicing and Advanced Slicing works.
Slice the word until first "a". (Tosc)
Slice the word so that you get "cana".
Now try to get "can" only.
Can you slice the word from beginning to the end with steps of 2 (including the last character.)?
Just keep in mind that in [start:stop] notation, stop value won’t be included. So, you need to type the index of the last value you’d like to include +1.
If you omit the parameter before the colon (:) It will mean all the way for that parameter.
So, [:3:1], here first colon has no parameter which means all the way from the beginning. And, [0::1] here second colon has no parameter which means all the way to the end.
ans_1 = wrd[::2]
ans_1 = wrd[0::2]
Now slice the word with steps of 2, excluding first and last characters.
Can you slice the list so that it's reversed without using the .reverse() method?
Slice the list so that only last 2 elements are included.
Slice the second and third elements (50 and 20) in the list.