Slicing in Python
You can slice pretty much any sequence in
Python. A sequence is something that you can index from 0 to len(sequence)-1.
Lists, tuples, and strings are all examples of sequences.
Syntax:
sequence[start:stop:step]
Let say we have a variable fruits that points to a
list:
If we put a colon and another number inside the square
brackets, we're slicing this list instead of indexing it:
Indexes start from 0. for example, from "Hello, World!" we need to extract "world" for that use sequence[7,12] as shown below.
In the second example, we have given step size as 2, so it will check for 2 step size. The output displayed as [1,3]
Comments
Post a Comment