Slicing is a concept to carve out a substring from a given string. Use slicing notation
s[start:stop:step] to access every
step-th element starting from index
start (included) and ending in index
stop (excluded). All three arguments are optional, so you can skip them to use the default values (
step=1). For example, the expression
s[2:4] from string
'hello' carves out the slice
'll' and the expression
s[:3:2] carves out the slice
Slicing is a Python-specific concept for carving out a range of values from sequence types such as lists or strings.
Try It Yourself:
Slicing is one of the most popular Python features. To master Python, you must master slicing first. Any non-trivial Python code base relies on slicing. In other words, the time you invest now in mastering slicing will be repaid a hundredfold during your career.
✅ Download: Free Coffee Break Python Slicing eBook
Here’s a 10-min video version of this article–that shows you everything you need to know about slicing:
[Intermezzo] Indexing Basics
To bring everybody on the same page, let me quickly explain indices in Python by example. Suppose we have a string
'universe'. The indices are simply the positions of the characters of this string.
The first character has index
0, the second character has index
1, and the
i-th character has index
Quick Introduction Slicing
💡 Info: The idea of slicing is simple. You carve out a subsequence from a sequence by defining the start and end indices. But while indexing retrieves only a single character, slicing retrieves a whole substring within an index range.
Use the bracket notation for slicing with the start and end position identifiers. For example,
word[i:j] returns the substring starting from index
i (included) and ending in index
j (excluded). Forgetting that the end index is excluded is a common source of bugs.
You can also skip the position identifier before or after the slicing colon. This indicates that the slice starts from the first or last position, respectively. For example,
word[:i] + word[i:] returns the same string as
Python puzzle 1: What is the output of this code snippet?
x = 'universe' print(x[2:4])
The Step Size in Slicing
For the sake of completeness, let me quickly explain the advanced slicing notation
[start:end:step]. The only difference to the previous notation is that it allows you to specify the step size as well.
For example, the command
'python'[:5:2] returns every second character up to the fourth character, i.e., the string
Python puzzle 2: What is the output of this code snippet?
x = 'universe' print(x[2::2])
Overshooting Indices in Slicing
Slicing is robust even if the end index shoots over the maximal sequence index. You only need to know that nothing unexpected happens if slicing overshoots sequence indices: it just slices to the maximum slice element.
Here is an example.
word = "galaxy" print(word[4:50])
Summary Python Slicing
Short recap, the slice notation s[start:end:step] carves out a substring from s. The substring consists of all characters between the two characters at the start index (inclusive) and the end index (exclusive). An optional step size indicates how many characters are left out from the original sequence. Here is an example:
s = 'sunshine' print(s[1:5:2]) #'us' print(s[1:5:1]) #'unsh'
Learn Slicing By Doing
Ok, so let’s train slicing a little bit. Solve the following puzzle in your head (and check the solution below).
Python Puzzle 3: What is the output of this code snippet?
# (Shakespeare) s = "All that glitters is not gold" print(s[9:-9]) print(s[::10]) print(s[:-4:-1])
Frequently Asked Questions
Let’s go a bit deeper into slicing to ensure you are getting it 100%.
I have searched the web to find all the little problems new Python coders face with slicing. I will answer six common questions next.
1) How to skip slicing indices (e.g., s[::2])?
The Python interpreter assumes certain default values for
s[start:stop:step]. They are:
step=1 (in the slice notation:
2) When to use the single colon notation (e.g., s[:]) and when double colon notation (e.g., s[::2])?
A single colon (e.g.
s[1:2]) allows for two arguments, the start and the end index. A double colon (e.g.
s[1:2:2]) allows for three arguments, the start index, the end index, and the step size. If the step size is set to the default value 1, we can use the single colon notation for brevity.
3) What does a negative step size mean (e.g., s[5:1:-1])?
This is an interesting feature in Python. A negative step size indicates that we are not slicing from left to right but from right to left. Hence, the start index should be larger or equal to the end index (otherwise, the resulting sequence is empty).
4) What are the default indices when using a negative step size (e.g., s[::-1])?
In this case, the default indices are not
end=len(s) but the other way round:
end=-1. Note that the start index is still included, and the end index is still excluded from the slice. Because of that, the default end index is -1 and not 0.
5) We have seen many examples of string slicing. How does list slicing work?
Slicing works the same for all sequence types. For lists, consider the following example:
l = [1, 2, 3, 4] print(l[2:]) # [3, 4]
Slicing tuples works in a similar way.
6) Why is the last index excluded from the slice?
The last index is excluded because of two reasons. The first reason is language consistency, e.g., the range function also does not include the end index. The second reason is clarity. Here’s an example of why it makes sense to exclude the end index from the slice.
customer_name = 'Hubert' k = 3 # maximal size of database entry x = 1 # offset db_name = customer_name[x:x+k]
Now suppose the end index would be included. In this case, the total length of the db_name substring would be k + 1 characters. This would be very counter-intuitive.
Here are the solutions to the puzzles in this article.
glitters is Al dlo
Subscribe to *FREE* Python Training Newsletter
And become a Python master on auto-pilot:
You can also download our free slicing ebook here:
✅ Download: Free Coffee Break Python Slicing eBook
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 that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a 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.