Within the information, we’ll illustrate getting the place of a component in Python.
Learn how to Discover an Index of the Aspect in Python?
Python gives completely different conditions the place you need to know the precise placement of a specific aspect inside an inventory or string. Python has a number of built-in strategies and capabilities for performing comparable duties.
Let’s undergo a number of examples of easy methods to retrieve the index of things utilizing numerous strategies.
Instance 1: Get the Place of an Aspect Utilizing the “index()” Methodology from the Python Checklist
The “index()” methodology built-in into Python gives a fundamental and simple approach to find the situation of a component in an inventory. If the aspect exists, it delivers the index of its preliminary incidence in any other case, it throws a ValueError.
First, declare an inventory variable named “mylist” after which, initialize it with the integer values. After that, create the “find_element” variable and assign it a desired aspect of the beforehand declared checklist:
mylist = [100, 200, 300, 400, 50]
find_element = 300
Now, invoke the “index()” methodology that takes the “find_element” variable as an argument to get its index and move it to the “position1” variable contained in the “attempt” block. Then, apply the “print()” assertion to get the resultant worth. If the desired aspect is just not discovered within the checklist, it’ll show the “ValueError”:
attempt:
position1 = mylist.index(find_element)
print(f“The Aspect {find_element} is at Place {position1}.”)
besides ValueError:
print(“Aspect not discovered within the Given checklist.”)
In response to the below-given output, the place of the desired aspect is “2”:
Methodology 2: Get the Index/Place of an Aspect Utilizing the “discover()” Methodology from Python String
Python consists of the “discover()” methodology for finding a substring inside a string. It returns the place of the substring’s first incidence. The primary distinction is that “discover()” returns “-1” if the enter substring is just not discovered.
Let’s transfer in direction of a easy instance.
Initially, declare the “mystring“ variable and initialize it with the string. Then, declared one other variable “sub_string” and move it to the substring:
sub_string = “Good”
find_position = mystring.discover(sub_string)
After that, use the “if else” situation to test the place of the supplied substring and name the “print()” assertion to show the place of the string:
if find_position != –1:
print(f“Given Substring ‘{sub_string}’ is on the Place {find_position}.”)
else:
print(“Substring not discovered within the string.”)
Output
Now we have compiled detailed info on easy methods to get a place of a component in Python.
Conclusion
Python has numerous strategies to retrieve the place of a component together with “index()” strategies in addition to the “discover()” methodology for getting the substring index worth. This information has demonstrated easy methods to get the place of a component in a specific kind of information in Python.