Home » Python

Slice Notation in Python

Python notation: Here, we are going to learn about the slice notation in Python with syntax and examples.
Submitted by Sapna Deraje Radhakrishna, on November 06, 2019

Python notation

The slice() functions returns a slice object. The slice() object is used to slice a given sequence (string, bytes, tuple, list or range) or any object which supports sequence protocol. Slicing is used to retrieve the subset of values. The basic slicing technique is to define a starting point.

Slice is a way to extract certain elements from data types like string and lists.

Syntax:

    slice[stop]
    slice[start:stop:step]

Other variations:

    slice[start:stop]
    slice[start:]
    slice[:stop]
    slice[:]
    slice[start:stop:step]

Parameter(s):

  • start: starting integer where the slicing of the object starts to.
  • stop: integer until which the slicing takes place. The slicing stops at index stop -1.
  • step: integer value which determines the increment between each index for slicing.
  • Note: If a single parameter is passed, start and step is None.

Example:

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> list = [1,2,3,4,5,6]
>>> print(list[3:5])
[4, 5] # here the elements from index 3 through 5 are extracted and added to list
>>> print(list[2:5:2])
[3, 5] # here the elements from index 2 through 5 with a step of 2 is extracted (every 2nd value)
>>> print(list[1:])
[2, 3, 4, 5, 6] # extract until the last index
>>> print(list[:3])
[1, 2, 3] # extract from the initial (0th ) index
>>>

Printing the values of list in a reverse order

Using the negative step value, we could print the items in the list in a reverse order.

Example:

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> list = [1,2,3,4,5,6]
>>> print(list[-1:1:-1])
[6, 5, 4, 3]
>>> print(list[::-1])
[6, 5, 4, 3, 2, 1] # reversed order of all elements in list
>>>

Slicing in string values

Slicing can also be applied on to a string variables. Consider the following examples,

Example 1: Reverse the string

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> test_str = "http://www.includehelp.com"
>>> print(test_str[::-1])
moc.plehedulcni.www//:ptth
>>>

Example 2: Get the top level domain

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> test_str = "http://www.includehelp.com"
>>> print(test_str[-4:])
.com
>>>

Example 3: Print the URL with the protocol

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> test_str = "http://www.includehelp.com"
>>> print(test_str[7:])
www.includehelp.com

Example 4: Print the URL without the protocol or top level domain

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> test_str = "http://www.includehelp.com"
>>> print(test_str[7:-4])
www.includehelp
>>>


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.