In this python tutorial, we will discuss indexing and slicing in Python with a few examples and also we will cover these below topics: Show
What is Indexing in python?
Positive indexing example in PythonIn Python Positive indexing, we pass a positive index that we want to access in square brackets. The index number starts from 0 which denotes the first character of a string. Example:
In this output, we can see the positive indexing example in python. You can refer to the below screenshot. Positive indexing example in PythonYou may like to read, Python program to find sum of n numbers and How to add two numbers in Python. Negative indexing example in PythonIn negative indexing in Python, we pass the negative index which we want to access in square brackets. Here, the index number starts from index number -1 which denotes the last character of a string. Example:
In this output, we can see a negative indexing example in python. You can refer to the below screenshot. Negative indexing example in PythonString indexing in python allows you to access individual characters from the string directly by using the index. Here, “(str[3])” is used to get “c”. Example:
In this output, we can see indexing in python string. You can refer to the below screenshot. You may like Python program to print element in an array. Indexing in python listPython List indexing can be done by accessing the list value by referring to its index number. Here, we have used “(list[2])” and it returns “Horse”. Example:
In this output, we can see indexing in python list. You can refer to the below screenshot. Indexing in python listIndexing in Python arrayArray indexing in python is the same as accessing an array element. Accessing an array element by referring to its index number. Here, we have used “(my_arr[1])” to access “12”. Example:
In this output, we can see indexing in the python array. You can refer to the below screenshot. Indexing in python arrayCheck out below Python array tutorials:
Indexing in python tupleWe will use the index operator “[]” to access an item from a Python tuple. Where index starts from 0. Here, we have used a negative index which starts from the right “(my_tuple[-2])” to get “8”. Example:
In this output, we can see indexing in python tuple. You can refer to the below screenshot. Indexing in python tuple
Indexing in a python dictionaryIndexing a dictionary in Python is to accesses the key or value at that index. Here, we will use list() to index the key of a Python dictionary. To get key at index in the list we have used “m_list[1]” and it will return “John”. Example:
In this output, we can see indexing in a python dictionary. You can refer to the below screenshot. Indexing in a python dictionary
What is slicing in python?Slicing in python is used for accessing parts of a sequence. The slice object is used to slice a given sequence or any object. We use slicing when we require a part of a string and not the complete string. Syntax:
Example:
In this output, we can see what is slicing in python. Here, the step is “-2” so from reverse every other value will be printed. You can refer to the below screenshot. What is slicing in pythonPython get substring using slice objectTo get the substring using slice object in python, we will use “slice(5)”. Here, start “0” and the end “5”, so it takes value one less than the end value. Example:
In this output, we can see how to get substring using slice object in python. Here, we can see the substring as “Guide”. You can refer to the below screenshot. Python get substring using slice objectPython get substring using negative indexTo get substring using negative index in python, we will use “slice(-1, -4, -1)”. Here, the start is “-1”, the end “-4”, and the step is negative “-1”. Example:
In this output, we can see how to get substring using negative index in python. Here, we can see the substring as “sed”. You can refer to the below screenshot. Python get substring using negative indexSlicing in Python listLets see slicing in python list. In this example, we have used “(my_list[-4::1])” for slicing in the list. So, the start is “-4” and we have not specified any value for the end so, by default it will take till last and “1” is step. Example:
In this output, we can see slicing in the python list. Here, we get the list in the output. You can refer to the below screenshot. Slicing in python list
Slicing in python stringSlicing in python string is used for getting a substring from a given string by slicing it. We have specified the start index as “3”, and the end index as “6” and it will return a string. Example:
In this output, we can see slicing in a python string. Here, we get the string as an output. You can refer to the below screenshot. Slicing in python string
Slicing in python tupleTo slice a tuple in Python, we will use “(my_tuple[4])”. To get the value we have to specify a positive integer, to fetch that index from the tuple counting from left. In case of negative index, we have to fetch that index from the right. Example:
In this output, we can see slicing in python tuple. Here, we get the two outputs. You can refer to the below screenshot. Slicing in python tuple
Slicing in Python arrayTo do slicing in the python array, we will import numPy as np. We will define as “(my_arr[1:])”. Here, the start is “1” and the end is not passed so, it will consider till the last length of an array. Example:
In this output, we can see slicing in the python array. Here, we get the output as an array. You can refer to the below screenshot. Slicing in python arrayIn this tutorial, we have learned about Indexing and slicing in Python with examples, and also we have covered these topics:
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile. What is list indexing and slicing with an example in Python?“Indexing” means referring to an element of an iterable by its position within the iterable. “Slicing” means getting a subset of elements from an iterable based on their indices. By way of analogy, I was recently summoned to jury duty, and they assigned each potential juror a number.
What is indexing in Python with example?The Python index() method helps you find the index position of an element or an item in a string of characters or a list of items. It spits out the lowest possible index of the specified element in the list. In case the specified item does not exist in the list, a ValueError is returned.
What is list slicing in Python with example?It's possible to "slice" a list in Python. This will return a specific segment of a given list. For example, the command myList[2] will return the 3rd item in your list (remember that Python begins counting with the number 0).
What is indexing in list in Python?Python index() is an inbuilt function in Python, which searches for a given element from the start of the list and returns the index of the first occurrence.
|