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
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
- numpy.index() in Python
- Python | Key index in Dictionary
- Python list | index()
- Python | Pandas Index.all()
- Python | Positional Index
- Python | Pandas Index.any()
- Python | Pandas Index.contains()
- Python | Pandas Index.where
- Python | Pandas Index.min()
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.