Slicing is one of the most popular Python features. Thus, understanding slicing is key to understand existing code bases. In this tutorial, I’m going to train your slicing skills. Ready? So, let’s go! ?
Watch the Video Python Slicing
Read About Slicing
Slicing is a Python-specific concept for accessing a range of values in sequence types such as lists or strings.
The idea of slicing is simple. Use the bracket notation to access a sequence of elements instead of only a single element. You do this via the colon notation of [start:end]
. This notation defines the start index (included) and the end index (excluded).
Solve the Slicing Code Puzzle
word = "galaxy" print(len(word[1:]))
What is the output of this code snippet?
More than one out of four Finxter users can not solve this puzzle. There are two concepts that are novel for them: the len()
function and slicing.
The len()
function is a handy tool to get the length of built-in Python data types such as strings, lists, dictionaries, or tuples. Learn it once and you will have an easier life in all future coding projects.
You can check whether you solved the puzzle correctly on the Finxter.com app: