- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
Python slice() Function
The slice() function in Python returns slice object. Basically it is used when we need to slice an object. For example:
a = [12, 23, 34, 45, 56, 67, 78, 89] x = slice(4) print(type(x))
The output is:
<class 'slice'>
Python slice() Function Syntax
The syntax of slice() function in Python, is:
slice(startIndex, stopIndex, step)
where:
- startIndex - refers to an integer value, that specifies from which index, the slicing to start
- stopIndex - refers to an integer value, that specifies the index number at where the slice to stop
- step - refers to an integer value, that specifies the step of slicing. For example, if 4 is given to step, then every fourth element will get returned
Note: The startIndex and step parameters are optional.
Note: The default value of startIndex is 0, where step is 1
Python slice() Function Example
Here is an example of slice() function in Python:
a = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] x = slice(4) print(a[x]) x = slice(1, 4) print(a[x]) x = slice(1, 8, 3) print(a[x]) str = "Python Programming is Fun!" sliceOb = slice(6) print(str[sliceOb])
The output is:
[11, 12, 13, 14] [12, 13, 14] [12, 15, 18] Python
Now the question is, what if there are multiple iterators available before the slice() function ?
Let's find out, using the program given below:
a = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] b = [21, 22, 23, 24, 25, 26, 27, 28, 29, 30] c = (31, 32, 33, 34, 35, 36, 37, 38, 39, 40) x = slice(4) print(a[x]) print(b[x]) print(c[x])
The output produced by this program is:
[11, 12, 13, 14] [21, 22, 23, 24] (31, 32, 33, 34)
Means that every object, that is a, b, and c are sliced and the slice object is x that holds the first four elements of respective object.
Use slice() to Slice using Negative Indexing - Backward Slicing
Here is an example, shows the use of slice() function to slice an object from backward, using negative indexing.
str = "Python Programming is Fun!" sliceOb = slice(-1, -5, -1) print(str[sliceOb])
The output is:
!nuF
Note: The -1 index number, refers to the last item.
« Previous Function Next Function »