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?
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:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.