In this tutorial you will understand slice notation works in Python, how to return a slice object and how to use slice object to specify how to slice a sequence.
Understand Slice Notation in Python
seq[ start: end: indexJump]
seq is the given sequence string or list, and in case of string the above syntax can be used to obtain substring i.e., portion of string starting from index “start” to end index “end” and index increment or jump size indexJump.
Note, the start is always included, end is always excluded. Therefore, seq[:i] + seq[i:] always results in seq.
Let’s say the given string is “python”.
>>> seq = "Python" >>> seq[:2]+seq[2:] 'Python'
>>> seq[:4]+seq[4:] 'Python'
useful defaults for omitted index
Note, Python assigns useful defaults for omitted slice indices. For instance, the omitted start index defaults to zero and the omitted end index defaults to size of the given sequence (list or string).
Therefore, you get the following output when you omit both start and end indices.
>>> seq[:] 'Python'
Positive and negative indexes and slicing notation
As per Python documentation, you need to think that indices (both positive and negative indexes) are pointing between characters as depicted in the following diagram.
Note, the first row in the above diagram indicates positive indexes position numbers from o to 6 for the string. And second row indicates the corresponding negative indexes. When you slice the string from index i to index j, it will consists of all the characters between i and j.
>>> seq[2:4] 'th' >>> seq[1:4] 'yth'
Similarly, let’s try negative indexes. Check the following examples negative indexes slicing.
>>> seq[-4:-2] 'th' >>> seq[-5:-2] 'yth'
Note, when you attempt to use an index value that is too large you will get the following error:
>>> seq Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range
However, when you use out of range indexes while slicing, it will be handled gracefully and you wont get any IndexError as shown below.
>>> seq[4:40] 'on' >>> seq[40:] '' >>> seq[40:40] ''
That’s it. Now you should have better understanding on slice notation in Python.
Hope it is helpful 🙂
You’ll also like:
- Convert list of lists to flat list – flatten list in Python ?
- String Formatting in Python – Examples
- Single digit number to double digits string conversion in Python
- String Immutability with Examples in Python
- Python Program to Check Given Number is Odd or Even
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Programs to Print Patterns – Pyramid, Triangle using Star
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- String Slicing in Python with Examples
- How to find index of an element in a list – Python ?
- Find difference between two given numbers in Python
- String indexing in Python with Examples
- Embed HTML within IPython Notebook
- Remove non-numeric characters from string in Python
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- Create list of lists in Python
- Extract numbers from a string in python
- How to access index and value in for loop – Python?
- Python Lists with Examples