HomeLinuxLearn how to Get a Place of Aspect in Python?

Learn how to Get a Place of Aspect in Python?


Components” are known as constructing blocks that signify single values within the information construction together with strings, checklist, and set. Varied operations, corresponding to entry location, manipulation, and elimination are carried out within the programming language on specific components of a given information set. Python additionally helps these options and one in every of them is to get the precise place of a component throughout the given information. For this specific objective, completely different built-in strategies/capabilities are utilized in Python.

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:

mystring = “At present is a Good Day”
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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments