Finding the string(substring) in a string is an application that has many uses in day-to-day life. Python offers this using a function index(), which returns the position of the first occurrence of substring in a string
Syntax : ch.index(ch1,begp,endp) Parameters : ch1 : The string to be searched for. begp (default : 0) : This function specifies the position from where search has to be started. endp (default : string_len) : This function specifies the position from where search has to end. Return Value : Returns the first position of the substring found. Exception : Raises ValueError if argument string is not found.
The first position of geeks after 2nd index : 8
Note : The index() method is similar to find(). The only difference is find() returns -1 if the searched string is not found and index() throws an exception in this case.
ValueError : This error is raised in the case when the argument string is not found in the target string.
Runtime Error :
Traceback (most recent call last): File "/home/aa5904420c1c3aa072ede56ead7e26ab.py", line 12, in pos = ch.index(ch1) ValueError: substring not found
Practical Application : This function is used to extract the suffix or prefix length after or before the target word. The example below displays the total bit length of instruction coming from AC voltage given information in string.
The total bit length of AC is : 13
- Python - Characters Index occurrences in String
- Python Language advantages and applications
- Python | Designing GUI applications Using PyQt
- isupper(), islower(), lower(), upper() in Python and their applications
- numpy string operations | index() function
- Python | Index of Non-Zero elements in Python list
- Python list | index()
- Python | Pandas Index.any()
- Python | Pandas Index.contains()
- Python | Positional Index
- Python | Pandas Index.max()
- Python | Pandas Index.min()
- Python | First alphabet index
- Python | Pandas Index.all()
- Python - Records with Value at K index
- Python | Pandas Index.where
- Python | Dictionary with index as value
- Readability Index in Python(NLP)
- numpy.index() in Python
- Python | Key index in Dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.